mad*_*per 0 php replace concat
我需要在随机字符串中替换和连接一些值,我将值存储在数组中.
即
$search = array('dog', 'tree', 'forest', 'grass');
$randomString = "A dog in a forest";
Run Code Online (Sandbox Code Playgroud)
如果一个或多个数组值与随机字符串匹配,那么我需要像这样的替换:
$replacedString = "A @dog in a @forest";
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?
谢谢.
foreach (explode(' ', $randomString) as $word) {
$replacedString .= in_array($word, $search) ? "@$word " : "$word ";
}
echo $replacedString; // A @dog in a @forest
Run Code Online (Sandbox Code Playgroud)