2013-10-19 22 views
-1

好吧,我认为我的问题的解决方案是使用REGEX,但我目前的知识阻碍了我实现我想要的结果。如何包装<a href>我查询中的所有URL

基本上,我如何在我的帖子/文本条目中使所有URL发生在被查询时包裹在<a href>元素内。

当我echo $content;从数据库中就会显示为:

this is a link http://somerandomsite.com, check it out! 

,但我怎样才能使一个文本中的链接时,显示将被包含在<a href>标签。

id老实说,我不知道如何正确编写语法。但这就是我想出的结果。

//get url from content 
$link = strstr($content, 'http://'); 

//insert <a href> for all $link inside $content 
//then display $content with all links wrapped in <href> 
+0

难道不是:“我有一个问题,我决定做一个正则表达式来解决这个问题现在我有2个问题。”?无论如何:如果你不知道语法应该是什么,最合理的步骤是首先[发现](http://www.php.net/manual/en/pcre.pattern.php)。在使用文档作为参考尝试了正则表达式之后再次提出您的问题。 – Sumurai8

回答

0
$a = 'this is a link http://somerandomsite.com, check it out!'; 

$a = preg_replace("/\b((?:http\:\/\/)|(?:www\.))([^ ,]+)/", "<a href=\"$1$2\">$1$2</a>", $a); 

echo $a; 
+0

谢谢你.. :) – bobbyjones

相关问题