我有preg_match_all和str_replace函数有点问题PHP preg_match_all和str_replace函数
<?
$source = 'hello @user_name, hello @user_name2, hello @user_name3';
preg_match_all("/@[\w\d_]*/s", $source, $search);
foreach($search[0] as $result) {
$source = str_replace($result, "<b>okay</b>", $source);
}
echo $source;
?>
结果是(错误):
hello <b>okay</b>, hello <b>okay</b>2, hello <b>okay</b>3
正确的结果应该是这样的:
hello <b>okay</b>, hello <b>okay</b>, hello <b>okay</b>
任何人都可以帮忙吗? 谢谢!
嗨Crontab,感谢您的建议..我尝试使用'$ source = preg_replace('/'.$ result。'/',''。$ result。' (',$ source);'但结果是一样的。:( –
我的意思是让你自己使用'preg_replace()'而不是使用'preg_match_all()'和'str_replace()'串联。 – Crontab
hmm,如果我不使用'preg_match_all',我不知道用什么字符串跟@ @必须替换为..?它怎么样? –