字符串中链接的正则表达式,仅在行尾排除点

时间:2012-05-29 08:16:52

标签: php regex

我创建了一个正则表达式,它读取一个字符串并将找到的url转换为HTML链接。我想排除一行末尾的点(包含文本链接),但它也排除了文本链接中的点(如http://www.website.com/page.html中所示。)此处的结束点应排除但不包括.html 。这是我的正则表达式:

    $text = preg_replace("#(^|[\n  \"\'\(<;:,\*])((www|ftp)\.+[a-zA-Z0-9\-_]+\.[^ \"\'\t\n\r< \[\]\),>;:.\*]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);                        

如何做到这一点?

感谢名单!汤姆

1 个答案:

答案 0 :(得分:4)

将您的RegEx更改为此

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

或此

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

<强>解释

"
\b                            # Assert position at a word boundary
(                             # Match the regular expression below and capture its match into backreference number 1
                                 # Match either the regular expression below (attempting the next alternative only if this one fails)
      http                          # Match the characters “http” literally
      s                             # Match the character “s” literally
         ?                             # 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
   |                             # Or match regular expression number 3 below (the entire group fails if this one fails to match)
      file                          # Match the characters “file” literally
)
://                           # Match the characters “://” literally
[-A-Z0-9+&@#/%?=~_|\$!:,.;]    # Match a single character present in the list below
                                 # The character “-”
                                 # A character in the range between “A” and “Z”
                                 # A character in the range between “0” and “9”
                                 # One of the characters “+&@#/%?=~_|\$!:,.;”
   *                             # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[A-Z0-9+&@#/%=~_|\$]           # Match a single character present in the list below
                                 # A character in the range between “A” and “Z”
                                 # A character in the range between “0” and “9”
                                 # One of the characters “+&@#/%=~_|\$”
"

希望这有帮助。