我並不完全清楚你的文字是什麼樣子的,vs你想要匹配的對象,但我會盡我所能去嘗試並把它做好。
基本上什麼,我這裏做的是尋找一個開放的鏈接標籤<a
,其次是一些東西(除了關閉HTML標記的任何東西),然後包裹在tildas ~
文本dead host
。接下來是關閉鏈接標記</a>
。
$string = "<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>";
if (preg_match('%<a[^>]*?~dead host~.*?</a>%i', $string)) {
print "Circle up the wagons - a match was found!";
}
else {
print "Let's pitch camp here - no match was found!";
}
這裏是正則表達式的解釋:
% <a [^>]*? ~dead host~ .*? </a> % i
^ ^ ^ ^ ^ ^^^
1 2 3 4 5 6 7 8
%
分隔符 - 告訴腳本,該模式從這裏開始。
<a
尋找一個開放的鏈接標籤。
[^>]*?
這是一個字符類[]
告訴腳本找到不是^
關閉html標記>
,多次,你可以*
,直到你打的表達?
下一部分的任何字符。在這種情況下,它會在發現~dead host~
時停止。這與項目#5類似,除了我們希望它匹配除關閉HTML標記以外的任何字符,而在編號#5中,它可以匹配任何字符,包括結束HTML標記。
~dead host~
查找包裹在tildas'〜'中的文字字符串'dead host'。
.*?
這意味着找到任何字符.
,儘可能多的*
,直到您碰到表達式?
的下一部分。在這種情況下,它是</a>
。
</a>
尋找閉合鏈接標籤。
%
定界符 - 告訴腳本模式在這裏結束。
i
模式修飾符 - 指示腳本忽略大小寫。如果您正在通過多行而不是僅僅一行進行搜索,則可能還需要添加ms
標誌。所以不是你的圖案修飾符看起來像這樣:i
,它看起來像這樣:ims
。雖然這在技術上並不正確,但一般來說,即使您有多行,也會將您的文本視爲一行。
希望這是你正在尋找。如果我不瞭解你在找什麼,請告訴我,我可以編輯它來調整它,以獲得你想要的。
Here is a working demo
編輯:
在回答您的意見,您可以使用preg_replace
代替preg_match
來代替的東西。
$string = "
<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
<a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
<a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
";
$string = preg_replace('%<a[^>]*?~dead host~.*?</a>%i', ' ', $string);
print $string;
這將用空格替換所有匹配而不是匹配它們。
Here is a working demo of the replacement
如果你正在處理的HTML數據,這將是更好地使用'DOM'。 – Passerby
感謝的建議,但會的preg_match是最好的選擇 – user3479821
http://stackoverflow.com/q/590747/570812 http://stackoverflow.com/q/1732348/570812 – Passerby