은 내가으로, 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>
누구든지 도움을받을 수 있습니까? 감사합니다. 첫 경기, @user_name
는 또한 @user_name2
및 @user_name3
(적어도 @user_name
부분)과 일치하기 때문에
안녕하세요, Crontab, 조언 해 주셔서 감사합니다. 나는'$ source = preg_replace ('/'.$ result. '/', ''. $ result. ' ', $ source);'결과는 동일 .. :( –
'preg_match_all()'과'str_replace()'를 직렬로 사용하는 대신에'preg_replace()'를 사용하는 것을 의미했습니다 – Crontab
hmm, 'preg_match_all'을 사용하지 않으면'@ '뒤에 어떤 문자열이 대체되어야하는지 모릅니다. 어떻게 되나요? –