[英]Regex replace word in string if it is not a link
我正在使用以下內容來查找和替換字符串$content
單詞:
$replacedcontent = preg_replace('/\b'.$term_name.'\b/u', '<a class="highlight">'.$term_name.'</a>', $content);
有沒有一種簡單的方法使用正則表達式僅在單詞沒有緊跟<a href="[link to somewhere]">
標記的情況下替換單詞?
使用否定的超前斷言。 這將替換錨標記中不存在的所有$term_name
。
preg_replace('/\b'.$term_name.'\b(?!(?:(?!<\/?a\b[^>]*>).)*?<\/a>)/u', '<a class="highlight">'.$term_name.'</a>', $content);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.