PHP:如何链接给定文本中的所有链接?

时间:2013-12-24 02:33:02

标签: php

我正在使用工具https://github.com/jmrware/LinkifyURL来检测文本单元中的网址。不幸的是,它只识别整个文本中的一个网址。例如,如果文本应该是:

  

http://www.guiageo-americas.com/imagens/imagem-america-do-sul.jpg我   真的认为这应该有效   http://www.youtube.com/watch?v=Cy8duEIHEig更多文字和一些写作   在这里和那里

出现的是:

  

http://www.guiageo-americas.com/imagens/imagem-america-do-sul.jpg 我   真的认为这应该有效   http://www.youtube.com/watch?v=Cy8duEIHEig更多文字和一些写作   在这里和那里

我想要的是:

  

http://www.guiageo-americas.com/imagens/imagem-america-do-sul.jpg 我   真的认为这应该有效    http://www.youtube.com/watch?v=Cy8duEIHEig 更多文字和一些写作   在这里和那里

有什么想法吗?当然,我会在这里留下PHP代码:

function linkify($text) {

 /* $text being "http://www.guiageo-americas.com/imagens/imagem-america-do-sul.jpg I really think this should be working http://www.youtube.com/watch?v=Cy8duEIHEig more text and some writing here and there" */

    $url_pattern = '/# Rev:20100913_0900 github.com\/jmrware\/LinkifyURL
    # Match http & ftp URL that is not already linkified.
      # Alternative 1: URL delimited by (parentheses).
      (\()                     # $1  "(" start delimiter.
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $2: URL.
      (\))                     # $3: ")" end delimiter.
    | # Alternative 2: URL delimited by [square brackets].
      (\[)                     # $4: "[" start delimiter.
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $5: URL.
      (\])                     # $6: "]" end delimiter.
    | # Alternative 3: URL delimited by {curly braces}.
      (\{)                     # $7: "{" start delimiter.
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $8: URL.
      (\})                     # $9: "}" end delimiter.
    | # Alternative 4: URL delimited by <angle brackets>.
      (<|&(?:lt|\#60|\#x3c);)  # $10: "<" start delimiter (or HTML entity).
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $11: URL.
      (>|&(?:gt|\#62|\#x3e);)  # $12: ">" end delimiter (or HTML entity).
    | # Alternative 5: URL not delimited by (), [], {} or <>.
      (                        # $13: Prefix proving URL not already linked.
        (?: ^                  # Can be a beginning of line or string, or
        | [^=\s\'"\]]          # a non-"=", non-quote, non-"]", followed by
        ) \s*[\'"]?            # optional whitespace and optional quote;
      | [^=\s]\s+              # or... a non-equals sign followed by whitespace.
      )                        # End $13. Non-prelinkified-proof prefix.
      ( \b                     # $14: Other non-delimited URL.
        (?:ht|f)tps?:\/\/      # Required literal http, https, ftp or ftps prefix.
        [a-z0-9\-._~!$\'()*+,;=:\/?#[\]@%]+ # All URI chars except "&" (normal*).
        (?:                    # Either on a "&" or at the end of URI.
          (?!                  # Allow a "&" char only if not start of an...
            &(?:gt|\#0*62|\#x0*3e);                  # HTML ">" entity, or
          | &(?:amp|apos|quot|\#0*3[49]|\#x0*2[27]); # a [&\'"] entity if
            [.!&\',:?;]?        # followed by optional punctuation then
            (?:[^a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]|$)  # a non-URI char or EOS.
          ) &                  # If neg-assertion true, match "&" (special).
          [a-z0-9\-._~!$\'()*+,;=:\/?#[\]@%]* # More non-& URI chars (normal*).
        )*                     # Unroll-the-loop (special normal*)*.
        [a-z0-9\-_~$()*+=\/#[\]@%]  # Last char can\'t be [.!&\',;:?]
      )                        # End $14. Other non-delimited URL.
    /imx';

       //below goes my code

    $url_replace = '$1$4$7$10$13<a style="color:blue;" onclick="toogleIframe(this)">$2$5$8$11$14</a>$3$6$9$12';
    //echo preg_replace($url_pattern, $url_replace, $text);
    return preg_replace($url_pattern, $url_replace, $text);
}

2 个答案:

答案 0 :(得分:3)

这是最好留给第三方图书馆的东西(你正在做的,所以很荣幸)。我推荐你自己尝试另一个。 purl是一个很好的选择。

答案 1 :(得分:0)

您可以使用以下内容替换正则表达式的所有匹配项(但是,我不会指望它的性能):

while (preg_match($pattern, $string)) {
    $string = preg_replace($pattern, $replacement, $string);
}

所以,你的功能将成为:

function linkify($text) {
    $url_pattern = "<your-pattern-string">;
    $url_replace = "<your-replacement-string">;
    while (preg_match($url_pattern, $url_replace, $text) {
        $text = preg_replace($url_pattern, $url_replace, $text);
    }
    return $text;
}