将文本链接替换为与preg_replace的链接

时间:2012-10-28 02:44:17

标签: php regex

我似乎遇到了一个问题,即将文本链接替换为发布网站的链接,而不是链接。

代码:

$status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text);
echo $status_text;

$status_text从名为contents的MySQL字段中提取,并包含其他文本,但我只是想链接链接。 另外,我也希望它不显示完整的网址,只显示主域名。

更新:我们在同一页面上还有另外两个preg_replaces,在它们前面找到链接到网站区域的+和#的东西,它们目前正在工作,不需要与上面的内容发生冲突:

$status_text = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://url.com/hashlink/$1\">$0</a>", $status_text);


$status_text = preg_replace("/\+([a-z_0-9]+)/i", "<a href=\"http://url.com/pluslink/$1\">$0</a>", $status_text);

2 个答案:

答案 0 :(得分:8)

在这里,试试这个:

$status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
echo $status_text;

或者让它更容易阅读:

$m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$r = '$1 <a href="$2">$3</a>';

$status_text = preg_replace($m,$r,$status_text);
echo $status_text;

编辑 - 由于OP中的新信息而更新 -

你的hashtag正则表达式没有考虑URL中的哈希值,所以让我们解决这个问题...... 此外,您应该在匹配hash-tags或plus-tags之前匹配URL,否则您将搞乱为hash-tags和plus-tags创建的链接

$status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);

或者让它更容易阅读...

$match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$2</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);

再次编辑 - 添加可能有用的网址

您可以在此处测试正则表达式:http://gskinner.com/RegExr/

另一个编辑

根据评论/问题,如果您想要考虑缺少http协议的网址,请使用以下内容:

$status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);

或者让它更容易阅读...

$match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$3</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);

示例用法

输入:(来自DB的文字 - &gt; $ status_text)

<!-- language-all: lang-none -->
Hi, this is an example. This is a url http://stackoverflow.com/ 
and this is a hash reference that we want to link to an internal 
post #AwesomePost123 and this one is a plus reference we want to 
link to an internal post +AwesomePost123 and finally an example 
of a url without the http protocol www.stackoverflow.com

输出:(运行正则表达式后)

<!-- language-all: lang-none -->
Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a> 
and this is a hash reference that we want to link to an internal 
post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to 
link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example 
of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a>

答案 1 :(得分:0)

你可以试试这个:

function makeClickableLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
}

要测试它,试试这个:

echo makeClickableLinks('Hello world, you go to www.google.com to check! also http://www.google.com/ works too!');

在这里演示:http://codepad.viper-7.com/hkOdCM