例:替換英文逗號,空格字符,或中文逗號
$str = '正確,聯絡我們goodsjob,goodsjob';
$reg = array(',',' ',',' );
$strs = ' username like %'.str_replace($reg,'%, or username like %',$str).'%';
回傳的資料是:username like %正確%,%, or username like %or%, or username like %username%, or username like %like%, or username like %%聯絡我們%, or username like %goodsjob%, or username like %goodsjob%,為什麼呢?
正確的結果應該是:usernme like %正確%, or username like %聯絡我們%, or username like %goodsjob%
例:替換英文逗號,空格字符,或中文逗號
$str = '正確,聯絡我們goodsjob,goodsjob';
$reg = array(',',' ',',' );
$strs = ' username like %'.str_replace($reg,'%, or username like %',$str).'%';
回傳的資料是:username like %正確%,%, or username like %or%, or username like %username%, or username like %like%, or username like %%聯絡我們%, or username like %goodsjob%, or username like %goodsjob%,為什麼呢?
正確的結果應該是:usernme like %正確%, or username like %聯絡我們%, or username like %goodsjob%
先把空格替換成其他字元 $str在替換的過程中,=》 %, or username like % ,這裡面也存在滿足你替換規則的字符串所以會影響到結果
<code> $str = '正确,联系我们 goodsjob,goodsjob'; $str = str_replace(' ', ' ', $str); $reg = array(',',','," "); $strs = 'username like %'.str_replace($reg,'%, or username like %',$str).'%'; </code>
注意:
由於str_replace()函數替換左到右,它可能會進行多次替換時替換先前插入的值。