简体   繁体   English

Linkify 正则表达式 Function PHP 大胆的火球方法

[英]Linkify Regex Function PHP Daring Fireball Method

So, I know there are a ton of related questions on SO, but none of them are quite what I'm looking for.所以,我知道有很多关于 SO 的相关问题,但没有一个是我正在寻找的。 I'm trying to implement a PHP function that will convert text URLs from a user-generated post into links.我正在尝试实现 PHP function,它将文本 URL 从用户生成的帖子转换为链接。 I'm using the 'improved' Regex from Daring Fireball towards the bottom of the page: http://daringfireball.net/2010/07/improved_regex_for_matching_urls The function does not return anything, and I'm not sure why.我在页面底部使用 Daring Fireball 的“改进”正则表达式: http://daringfireball.net/2010/07/improved_regex_for_matching_urls function 不返回任何内容,我不确定为什么。

<?php
if ( false === function_exists('linkify') ):   
  function linkify($str) {
$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';     
return preg_replace($pattern, "<a href=\"\\0\" rel=\"nofollow\" target=\"_blank\">\\0</a>", $str);      
}
endif;
?>

Can someone please help me get this to work?有人可以帮我解决这个问题吗? Thanks!谢谢!

Try this:试试这个:

$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';     
return preg_replace("!$pattern!i", "<a href=\"\\0\" rel=\"nofollow\" target=\"_blank\">\\0</a>", $str); 

PHP's preg function do need delimiters . PHP 的preg function 确实需要定界符 The i at the end makes it case-insensitive最后的i使其不区分大小写

Update更新

If you use # as the delimiter, you wan't need to escape the !如果您使用#作为分隔符,则无需转义! in the pattern as such use the original pattern string (the pattern does not have a # ): "#$pattern#i"在这样的模式中使用原始模式字符串(模式没有# ): "#$pattern#i"

Update 2更新 2

To ensure that the links are correct, do this:为确保链接正确,请执行以下操作:

$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
return preg_replace_callback("#$pattern#i", function($matches) {
    $input = $matches[0];
    $url = preg_match('!^https?://!i', $input) ? $input : "http://$input";
    return '<a href="' . $url . '" rel="nofollow" target="_blank">' . "$input</a>";
}, $str); 

This will now append http:// to the urls so that browser doesn't think it is a relative link.这现在将 append http://指向 url,这样浏览器就不会认为它是相对链接。

I was looking to just get the urls from a string using the same regex from the answer above by d_inevitable and wasn't looking to turn them into links or care about the rest of the string, I only wanted the urls with in the string so this is what I did.我只是想从d_inevitable上面的答案中使用相同的正则表达式从字符串中获取 url,而不是想将它们变成链接或关心字符串的 rest,我只想要字符串中的 url 所以这就是我所做的。 Hope it helps.希望能帮助到你。

/**
 * Returns the urls in an array from a string.
 * This dos NOT return the string, only the urls with-in.
 */
function get_urls($str){

    $regex = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
    preg_match_all("#$regex#i", $str, $matches);
    $urls = $matches[0];
    return $urls;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM