PHP - preg_replace - 文本链接作为可点击链接

时间:2016-03-07 19:03:15

标签: php

我使用此preg_replace将文本网址更改为可在我的脚本中点击:Replacing Text link as link with preg_replace

昨天我测试了我的脚本,这些preg_replace没有用。

以下是示例:

(...) stronie  http://tpn.pl/nowosci/uwaga-skiturowcy-1).

这些preg_replace最终创建了这个:

stronie <a href="http://tpn.pl/nowosci/uwaga-skiturowcy-1)." target="_blank">tpn.pl</a>

为什么这个preg_replace添加&#34;)。&#34;在URL的末尾?如何只接受&#34; A-Z,a-z,0-9,/&#34;在URL字符串的末尾(我想我写了一切接受url的结尾?)?

感谢。

编辑:

这是我的代码:

$sub_message = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a target="_blank" href="$2">$3</a>', $value['description']);
$sub_message = trim(str_replace("\n", "<br />", $sub_message));

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码将URL更改为字符串中的链接:

<?php

$x="foobar http://example.com foobar";
echo preg_replace("~(https?://(?:www\.)?[^\s]+)~i","<a href='$1'>$1</a>",$x);
  

在线演示:https://eval.in/532195

相关问题