Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式php中的任何URL或链接_Php_Regex_Url_Hyperlink - Fatal编程技术网

正则表达式php中的任何URL或链接

正则表达式php中的任何URL或链接,php,regex,url,hyperlink,Php,Regex,Url,Hyperlink,我使用正则表达式捕获php中的任何url: ((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?) 但它没有捕获完整的url。。。。。比如说 在这里: 它只捕捉到: 那么我需要什么来捕获完整的url呢?您

我使用正则表达式捕获php中的任何url:

((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)
但它没有捕获完整的url。。。。。比如说

在这里:

它只捕捉到:


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

您需要在正则表达式中添加一个coma字符:

您的正则表达式已修复:

((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@,.\w_]*)#?(?:[\w]*)?))
验证正则表达式的好站点:


如果要将URL分解为多个部分,可以使用parse_URL()PHP函数

可以尝试以下方法:

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


您需要该正则表达式做什么?这些URL不一样?你能提供一个真实的例子吗?
<!--
\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 “+&@#/%=~_|$” «+&@#/%=~_|$»
-->