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

PHP:如何链接给定文本中的所有链接?,php,Php,我正在使用该工具检测文本单元中的URL。不幸的是,它只识别整个文本中的一个URL。例如,如果文本应为: http://www.guiageo-americas.com/imagens/imagem-america-do-sul.jpg 我 我真的认为这应该有效 http://www.youtube.com/watch?v=Cy8duEIHEig 更多的文字和一些文字 到处 出现的是: I 我真的认为这应该有效 http://www.youtube.com/watch?v=Cy8duEIHEig

我正在使用该工具检测文本单元中的URL。不幸的是,它只识别整个文本中的一个URL。例如,如果文本应为:

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

出现的是:

I 我真的认为这应该有效 http://www.youtube.com/watch?v=Cy8duEIHEig 更多的文字和一些文字 到处

我想要的是:

I 我真的认为这应该有效 更多文本和一些文字 到处

你知道为什么吗?当然,我将把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);
}
函数linkify($text){
/*$text正在运行”http://www.guiageo-americas.com/imagens/imagem-america-do-sul.jpg 我真的认为这应该有效http://www.youtube.com/watch?v=Cy8duEIHEig 更多的文字和一些文字在这里和那里”*/
$url_pattern='/#版本:20100913_0900 github.com\/jmrware\/LinkifyURL
#匹配尚未链接的http和ftp URL。
#备选方案1:URL由(括号)分隔。
(\()\$1)(“开始分隔符。
((?:ht|f)tps?:\/\/[a-z0-9\-.\!$&'()*+,;=:\/?[\]@%]+)\$2:URL。
(\)\$3:“)”结束分隔符。
|#备选方案2:URL由[方括号]分隔。
(\[)\$4:“[”开始分隔符。
((?:ht|f)tps?:\/\/[a-z0-9\-.\!$&'()*+,;=:\/?[\]@%]+)\$5:URL。
(\])\$6:“]”结束分隔符。
|#备选方案3:URL由{花括号}分隔。
(\{)\$7:“{”开始分隔符。
((?:ht|f)tps?:\/\/[a-z0-9\-.\!$&'()*+,;=:\/?[\]@%]+)\$8:URL。
(\})\$9:“}”结束分隔符。
|#备选方案4:URL由分隔符分隔。

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

因此,您的功能将变为:

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;
}
函数linkify($text){

$url_pattern=“这是最好留给第三方库做的事情(你正在这么做,很荣幸)。我建议你在自己动手之前再尝试一个。这是一个很好的选择。

只是一个评论,说这是一个非常棒的正则表达式。哇!@JakeGould确实如此!我并不认识这个人(杰夫·罗伯森)但他确实很擅长:pTy是给你的提示,但你真的不能想出一个解决办法使用这个吗?这是我真正想要的,因为这几乎是完美的功能,我不想冒险使用另一个模块:pAlmost工作到完美和实际工作往往相距很远。我的建议是:find一个实际工作的不同库。这可能是一个研究的好时机。交换库对Composer来说是轻而易举的事。
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;
}