PHP Linkify内容链接

PHP Linkify内容链接,第1张

PHP Linkify内容链接

我在GitHub上有一个开源项目:linkifyURL,您可能要考虑一下。它具有一个功能:

linkify()
从文本中提取URL并将其转换为链接。请注意,这不是一项微不足道的任务!

如果您确实不需要链接特定的域(即vimeo和youtube),则可以使用以下修改的PHP函数

linkify_filtered
(以有效的测试脚本形式)来满足您的需要:

<?php // test.php 20110313_1200function linkify_filtered($text) {    $url_pattern = '/# Rev:20100913_0900 github.com/jmrware/linkifyURL    # Match http & ftp URL that is not already linkified.      # Alternative 1: URL delimited by (parentheses).      (()          #   "(" start delimiter.      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # : URL.      ())          # : ")" end delimiter.    | # Alternative 2: URL delimited by [square brackets].      ([)          # : "[" start delimiter.      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # : URL.      (])          # : "]" end delimiter.    | # Alternative 3: URL delimited by {curly braces}.      ({)          # : "{" start delimiter.      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # : URL.      (})          # : "}" end delimiter.    | # Alternative 4: URL delimited by <angle brackets>.      (<|&(?:lt|#60|#x3c);)  # : "<" start delimiter (or HTML entity).      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # : URL.      (>|&(?:gt|#62|#x3e);)  # : ">" end delimiter (or HTML entity).    | # Alternative 5: URL not delimited by (), [], {} or <>.      (  # : 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 . Non-prelinkified-proof prefix.      ( b          # : 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 . Other non-delimited URL.    /imx';//    $url_replace = '<a href=""></a>';//    return preg_replace($url_pattern, $url_replace, $text);    $url_replace = '_linkify_filter_callback';    return preg_replace_callback($url_pattern, $url_replace, $text);}function _linkify_filter_callback($m){ // Filter out youtube and vimeo domains.    $pre  = $m[1].$m[4].$m[7].$m[10].$m[13];    $url  = $m[2].$m[5].$m[8].$m[11].$m[14];    $post = $m[3].$m[6].$m[9].$m[12];    if (preg_match('/b(?:youtube|vimeo).comb/', $url)) {        return $pre . $url . $post;    } // else linkify...    return $pre .'<a href="'. $url .'">' . $url .'</a>' .$post;}// Create some test data.$data = 'Plain URLs (not delimited):foo http://example.com bar...foo http://example.com:80 bar...foo http://example.com:80/path/ bar...foo http://example.com:80/path/file.txt bar...foo http://example.com:80/path/file.txt?query=val&var2=val2 bar...foo http://example.com:80/path/file.txt?query=val&var2=val2#fragment bar...foo http://example.com/(file's_name.txt) bar... (with ' and (parentheses))foo http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7348] bar... ([IPv6 literal])foo http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7348]/file.txt bar... ([IPv6] with path)foo http://youtube.com bar...foo http://youtube.com:80 bar...foo http://youtube.com:80/path/ bar...foo http://youtube.com:80/path/file.txt bar...foo http://youtube.com:80/path/file.txt?query=val&var2=val2 bar...foo http://youtube.com:80/path/file.txt?query=val&var2=val2#fragment bar...foo http://youtube.com/(file's_name.txt) bar... (with ' and (parentheses))foo http://vimeo.com bar...foo http://vimeo.com:80 bar...foo http://vimeo.com:80/path/ bar...foo http://vimeo.com:80/path/file.txt bar...foo http://vimeo.com:80/path/file.txt?query=val&var2=val2 bar...foo http://vimeo.com:80/path/file.txt?query=val&var2=val2#fragment bar...foo http://vimeo.com/(file's_name.txt) bar... (with ' and (parentheses))';// Verify it works...echo(linkify_filtered($data) ."n");?>

这采用了回调函数来进行过滤。是的,正则表达式很复杂(但事实证明是个问题!)。您可以

linkify()
在此处查看交互式Java语言版本的实际 *** 作:URLlinkification(HTTP /FTP)。

另外,JohnGruber有一个很好的正则表达式来进行链接化。请参阅:改进的自由,准确的正则表达式模式,用于匹配URL。但是,他的正则表达式在某些情况下会遭受_灾难性的回溯_ 。(有关此事,我已经写信给他,但他尚未回复。)

希望这可以帮助!:)



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5152392.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存