2015-10-20 48 views
0

Basicaly,我发现了一些代码在这里:PHP不完全linkifying

http://buildinternet.com/2010/05/how-to-automatically-linkify-text-with-php-regular-expressions/

其中全自动转文本链接到点击的链接。

然而,有一个错误,我不能发现,因为我不是很好的正则表达式,它是不完全捕获文本或类似的东西。如图所示:

https://gyazo.com/f279a3a2bedd8a078713d45f971ab65d

代码:

function link_filter_offence($player_complaints_offence_raw) 
     { 
      $player_complaints_offence_raw= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" >$3</a>", $player_complaints_offence_raw); 
      $player_complaints_offence_raw= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" >$3</a>", $player_complaints_offence_raw); 
      $player_complaints_offence_raw= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:[email protected]$3\">[email protected]$3</a>", $player_complaints_offence_raw); 
      return($player_complaints_offence_raw); 
     } 

$player_complaints_offence_raw = 'https://www.youtube.com/watch?v=klXTQjmfVeg&feature=youtu.be'; 
$player_complaints_offence_raw = link_filter_offence($player_complaints_offence_raw); 

$reply = html_entity_decode($player_complaints_offence_raw); 
echo $reply; 

很抱歉,如果我不能更详细一些,但我不知道如何在这一刻来解决这个时间。

+0

在正则表达式,你会看到.. \ n \ r \ t < .... <应该是一个<。它看起来像你复制的HTML源代码,它改变<< – kainaw

+0

你也可以在你使用的例子中没有正确编码html,只是将它改为'<'。 – Flosculus

回答

0

试试这个

function link_filter_offence($player_complaints_offence_raw) 
     { 
$player_complaints_offence_raw= 
preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" >$3</a>", $player_complaints_offence_raw); 
$player_complaints_offence_raw= 
preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" >$3</a>", $player_complaints_offence_raw); 
$player_complaints_offence_raw= 
preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:[email protected]$3\">[email protected]$3</a>", $player_complaints_offence_raw); 
      return($player_complaints_offence_raw); 
     } 

$player_complaints_offence_raw = 'https://www.youtube.com/watch?v=klXTQjmfVeg&feature=youtu.be'; 
$player_complaints_offence_raw = link_filter_offence($player_complaints_offence_raw); 

$reply = html_entity_decode($player_complaints_offence_raw); 
echo $reply; 


所有我做的是,我有逃脱你的preg_replace函数的第二个参数,它是完美的作品对我来说

+0

非常感谢,现在也适用于我! – Conner