0
我有這個突出顯示關鍵字的函數。它工作的很好,但它似乎不適用於單引號和雙引號。如何在突出顯示關鍵字的函數中支持引號
function highlight($input, $keywords) {
// Hello Mr Regex
preg_match_all('~\w+~', $keywords, $match);
// If there's no match
if(!$match) { return $input; }
// Case sensitive search
//$result = '~\\b(' . implode('|', $match[0]) . ')\\b~';
// Case insenstive search
$result = '~\\b(' . implode('|', $match[0]) . ')\\b~i';
// Return highlighted text
return preg_replace($result, '<strong>$0</strong>', $input);
}
$keywords = "just another what's little's test";
$input = "here what what's just looking little's another's this's for another one that requires a test";
結果:這裏什麼什麼的只是尋找一點是另一個人的這的爲另一個一個需要測試
在這個例子中,它不應該被突出的上這是
我使用htmlspecialchars()
也嘗試兩個輸入和關鍵字,但這似乎也沒有得到匹配。
任何想法?
完美,謝謝! – 2013-03-16 19:17:49
你能推薦學習正則表達式的好資源嗎? – 2013-03-16 19:18:05
@Titanium http://www.regular-expressions.info/似乎是每個人的最愛 – 2013-03-16 19:19:03