regex any urls or links in php

前端 未结 2 1508
臣服心动
臣服心动 2021-01-17 07:54

I have that regex to catch any url in php :

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


        
相关标签:
2条回答
  • 2021-01-17 08:19

    You need to add a coma character in regex:

    Your regex fixed:

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

    Good site to validate regex: Rubular

    If you want decompose the URL into a parts, you can use the parse_url() PHP function

    0 讨论(0)
  • 2021-01-17 08:19

    May try this one:

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

    Explanation

    <!--
    \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 “+&@#/%=~_|$” «+&@#/%=~_|$»
    -->
    
    0 讨论(0)
提交回复
热议问题