2014-03-13 104 views
0

斯洛伐克語中有兩個非常相似的表達式存在問題。我試圖替換html鏈接的表達式,下面是我的示例代碼。我在陣列中有大約90個字。preg_replace與兩個相似的單詞

// mysql query above. 
for($i=1;$i<=$datas[1];$i++) { 
    $regex[] = '/('.htmlentities($rows['title'], ENT_QUOTES, 'UTF-8').')/i'; 
    $wrl[] = '<a href="'.URL.'link/'.urlencode($rows['alias']).'" class="underscored">'.strtolower($rows['title']).'</a>'; 
} 
$content = preg_replace($regex, $wrl, $content); 

但我有一個非常相似的單詞的問題; (nenasytene mastne kyseliny,nasytene mastne kyseliny)。 preg_replace從較長的單詞中刪除ne,這會導致單詞鏈接到錯誤的文章。

回答

1

嘗試修改你的正則表達式之前指定一個字邊界\b和短語之後,你想用一個鏈接來代替:

$regex[] = '/\b('.htmlentities($rows['title'], ENT_QUOTES, 'UTF-8').')\b/i'; 
+0

非常感謝。它完美無瑕。 – InsaneSVK

0

您可以預先準備並與\W添加您正則表達式:

$regex[] = '/\W('.htmlentities($rows['title'], ENT_QUOTES, 'UTF-8').')\W/i'; 
+0

非常感謝您的建議。 – InsaneSVK

相關問題