1

文字列を読み取り、見つかった 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);                        

どうすればそれを行うことができますか?

ありがとう!トム

4

1 に答える 1

4

正規表現をこれに変更します

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

お役に立てれば。

于 2012-05-29T08:20:38.417 に答える