我要找到一个字符串所需单词,然后我告诉马克或突出如何找到搜索词后位置转换为字符串标注这一
$str = "this is a test for testing a test function";
$array = explode(' ',$str);
$key = array_search('tes', $array);
$tr='';
foreach($array as $i=>$ar){
if($key == $i){
$tr .= '<a style="color:red">'.$ar.'</a> ';
}else{
$tr .= $ar.' ';
}
}
echo $tr;//this is a <a style="color:red">test</a> for <a style="color:red">testing</a> a <a style="color:red">test</a> function
搜索 - >“TES”;
回声发现后 - >这是一个“测试”“测试”“测试”功能
什么是你的期望输出 –
问题是什么? – RomanPerekhrest
为什么不只是'str_replace('tes','tes',$ str)'? –