2014-04-01 122 views
0

我想在PHP中使用preg_match來測試一個URL的格式。該URL看起來是這樣的:preg_match在網址中的特定模式?

<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a> 

以及老實說,我沒有任何的preg_match創造的想法,但我的目標是

模式開始<a href= contain word ~dead host~ end with </a>

我嘗試字符串包含在PHP自身的功能,但遺憾的是它不聰明,所以我認爲preg_match是唯一的選擇。

+3

如果你正在處理的HTML數據,這將是更好地使用'DOM'。 – Passerby

+0

感謝的建議,但會的preg_match是最好的選擇 – user3479821

+0

http://stackoverflow.com/q/590747/570812 http://stackoverflow.com/q/1732348/570812 – Passerby

回答

0

我並不完全清楚你的文字是什麼樣子的,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 
  1. %分隔符 - 告訴腳本,該模式從這裏開始。
  2. <a尋找一個開放的鏈接標籤。
  3. [^>]*?這是一個字符類[]告訴腳本找到不是^關閉html標記>,多次,你可以*,直到你打的表達?下一部分的任何字符。在這種情況下,它會在發現~dead host~時停止。這與項目#5類似,除了我們希望它匹配除關閉HTML標記以外的任何字符,而在編號#5中,它可以匹配任何字符,包括結束HTML標記。
  4. ~dead host~查找包裹在tildas'〜'中的文字字符串'dead host'。
  5. .*?這意味着找到任何字符.,儘可能多的*,直到您碰到表達式?的下一部分。在這種情況下,它是</a>
  6. </a>尋找閉合鏈接標籤。
  7. %定界符 - 告訴腳本模式在這裏結束。
  8. 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

+0

工作,我看到了所有的回覆內容。首先我很驚訝。這這個代碼是完整的,我按照我的想法工作。不需要任何改變ps:任何想法,如果有100個鏈接一半的他們都喜歡串(以上預浸比賽),我想用空間如http://pastebin.com/CfQdrihS – user3479821

+0

是的,這是很容易做到的更換。您可以使用'preg_replace'來代替使用'preg_match'。我已經對上面的代碼進行了編輯,向您展示瞭如何實現。 – Quixrick

0
如果你想只匹配URL

$text="<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>"; 

preg_match_all("/http:\/\/ ## starting from http:// 
~dead\shost~ ## along with http:// match ~dead host~ 
[^\"']   ## upto singlequote or doublequote 
+    ## one more character 
/mx",$text,$matches); // m - multiple line x - include to commentary inside patterns 
print_r($matches); 

工作Demo