0
我想弄清楚一個程序員卡在preg_match上的客戶端的問題。我並不擅長這些,但我的解決方案顯然不起作用。這是他的要求:preg_match援助需要5
非常簡單的工作。需要一個正則表達式preg_match,它匹配所有不在html標籤或鏈接錨文本部分的字符串。
例如,如果我們有字符串:
Blah blah needle blah blah <div id='needle'>blah blah <a href='#'>needle</a> blah needle</div>
中的preg_match應該只找針的兩個實例。
這裏是我的解決方案,它並沒有爲他們的工作需要:
<?php
// The string
$string = "Blah blah needle blah blah <div id='needle'>blah blah <a href='#'>needle</a> blah needle</div>";
// You need everything outside of the tags, so let's get rid of the tags
// and everything in between.
$new_string = preg_replace("/<.*>.*<\/.*>/msU","",$string);
// Now let's match 'needle'
preg_match_all("/needle/msU",$new_string,$matches);
var_export($matches);
?>
有人告訴我,它沒有工作,因爲它「匹配,因此 結果爲未格式化的HTML刪除之前所有的HTML」。我不知道他們爲什麼不能做$ string2 = $ string;並將HTML字符串存儲在其他地方供以後使用。我也不知道爲什麼這很重要,因爲它只是一個preg_match而不是他們正在尋找的preg_replace。我想如果有人可以幫助一個單線preg_match_all什麼的,我會非常感激。
感謝;]
卡西米爾 - 完美的作品!非常感謝 ;] – Adam