我想匹配兩個字符串,如果有任何單詞匹配我想爲它們添加標記。在php中比較兩個字符串並添加標記
我試過類似下面下面的代碼
$old = "one two three";
$new = "how is one related to two";
foreach (explode(" ", $old) as $str) {
$str .= str_replace($str, "<b>$new</b>", $str);
}
echo trim($str);
Exptected結果how is <b>one</b> related to <b>two</b>
。
如果可能的話,請給我建議一些其他的方式,如循環。如果不可能,請告訴我循環。
嘿感謝!沒有循環可能嗎?我必須在單個頁面中檢查1000個句子....或僅循環是要走的路? – Vishnu
要檢查的文本數量不是問題,循環只會通過向'$ old'添加更多單詞來獲得更多迭代。 –
好.. +1 :)接受 – Vishnu