2012-05-17 70 views
-1

任何URL或鏈接我有一個正則表達式來趕在PHP中的任何網址:正則表達式在PHP

((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w][email protected])?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w][email protected])[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?) 

,但它沒有趕上完整的URL .....爲例

在這裏:

http://v12.lscache6.c.youtube.com/videoplayback?app=youtube_gdata&devkey=AX8iKz393pCCMUL6wqrPOZoO88HsQjpE1a8d1GxQnGDm&el=videos&upn=0K3DA3wYhjI&uaopt=no-save&source=youtube&itag=18&id=ab59b1e9554eca6d&ip=0.0.0.0&ipbits=0&expire=1339342342&sparams=id,itag,source,uaopt,upn,ip,ipbits,expire&signature=5026BE137B41D5CD9785E752D1892903D432974C.BA1D4E0C138210B2275391A2A3D469E582183621&key=yta1&cms_redirect=yes 

只抓到:

http://v12.lscache6.c.youtube.com/videoplayback?app=youtube_gdata&devkey=AX8iKz393pCCMUL6wqrPOZoO88HsQjpE1a8d1GxQnGDm&el=videos&upn=0K3DA3wYhjI&uaopt=no-save&source=youtube&itag=18&id=ab59b1e9554eca6d&ip=0.0.0.0&ipbits=0&expire=1339342342&sparams=id

那麼,我需要什麼來捕捉完整的網址?

+0

對於你需要什麼是正則表達式? – CodeZombie

+1

那些網址不一樣?你能提供一個真實的例子嗎? – afuzzyllama

回答

1

你需要在正則表達式添加昏迷字符:

你的正則表達式固定:

((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w][email protected])?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w][email protected])[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@,.\w_]*)#?(?:[\w]*)?)) 

好發部位,以驗證正則表達式:Rubular

如果你想分解URL到的部分,你可以使用parse_url()PHP函數

+0

幹得好!你是regexps的主人:) – gaussblurinc

0

可以試試這個:

\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$] 

說明

<!-- 
\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$] 

Options: case insensitive 

Assert position at a word boundary «\b» 
Match the regular expression below «(?:(?:https?|ftp|file)://|www\.|ftp\.)» 
    Match either the regular expression below (attempting the next alternative only if this one fails) «(?:https?|ftp|file)://» 
     Match the regular expression below «(?:https?|ftp|file)» 
     Match either the regular expression below (attempting the next alternative only if this one fails) «https?» 
      Match the characters 「http」 literally «http» 
      Match the character 「s」 literally «s?» 
       Between zero and one times, as many times as possible, giving back as needed (greedy) «?» 
     Or match regular expression number 2 below (attempting the next alternative only if this one fails) «ftp» 
      Match the characters 「ftp」 literally «ftp» 
     Or match regular expression number 3 below (the entire group fails if this one fails to match) «file» 
      Match the characters 「file」 literally «file» 
     Match the characters 「://」 literally «://» 
    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «www\.» 
     Match the characters 「www」 literally «www» 
     Match the character 「.」 literally «\.» 
    Or match regular expression number 3 below (the entire group fails if this one fails to match) «ftp\.» 
     Match the characters 「ftp」 literally «ftp» 
     Match the character 「.」 literally «\.» 
Match a single character present in the list below «[-A-Z0-9+&@#/%=~_|$?!:,.]*» 
    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
    The character 「-」 «-» 
    A character in the range between 「A」 and 「Z」 «A-Z» 
    A character in the range between 「0」 and 「9」 «0-9» 
    One of the characters 「+&@#/%=~_|$?!:,.」 «+&@#/%=~_|$?!:,.» 
Match a single character present in the list below «[A-Z0-9+&@#/%=~_|$]» 
    A character in the range between 「A」 and 「Z」 «A-Z» 
    A character in the range between 「0」 and 「9」 «0-9» 
    One of the characters 「+&@#/%=~_|$」 «+&@#/%=~_|$» 
-->