【问题标题】:regex any urls or links in php正则表达式 php 中的任何 url 或链接
【发布时间】:2012-05-17 15:56:00
【问题描述】:

我有那个正则表达式来捕获 php 中的任何 url:

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

但它没有捕捉到完整的网址.....例如

这里:

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

那么我需要什么来获取完整的 url?

【问题讨论】:

  • 你需要那个正则表达式做什么?
  • 那些网址不一样?能举个真实的例子吗?

标签: php regex url hyperlink


【解决方案1】:

你需要在正则表达式中添加一个逗号:

您的正则表达式已修复:

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

验证正则表达式的好网站:Rubular

如果要将 URL 分解成部分,可以使用 parse_url() PHP 函数

【讨论】:

    【解决方案2】:

    可以试试这个:

    \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 “+&@#/%=~_|$” «+&@#/%=~_|$»
    -->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多