Php 将http://[…]替换为链接文本中没有http://的链接

Php 将http://[…]替换为链接文本中没有http://的链接,php,hyperlink,preg-replace,Php,Hyperlink,Preg Replace,我使用这个preg_replace来查找tweet文本中的链接,它会尽职尽责地返回一个HTML链接,但我想知道是否有可能在同一行中丢失链接文本中的http://链接。例如: $output .= preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a target="blank" rel="nofollow" href="$1" target="_blank">

我使用这个preg_replace来查找tweet文本中的链接,它会尽职尽责地返回一个HTML链接,但我想知道是否有可能在同一行中丢失链接文本中的http://链接。例如:

$output .= preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a target="blank" rel="nofollow" href="$1" target="_blank">$1</a>', $status->text);
$output.=preg\u replace('@(https?:/([-\w\.]+[-\w])+(:\d+)(/([\w/-]*(\?\S+?[^\.\S]))?)@,'.$status->text);
可能会返回:

<a target="blank" rel="nofollow" href="https://kanyewest.com/">https://kanyewest.com</a>

但我真的希望它能回来:

<a target="blank" rel="nofollow" href="https://kanyewest.com/">kanyewest.com</a>


您可以只使用一个子字符串

看看这个函数,它工作得很好,@Charlotte,只是它去掉了链接的第二部分,后面有一个“/”。例如,“t.co/twJ6g0rqtz”在链接文本中返回时仅为“t.co”。请在Replace参数内将
$4
替换为
$4$5
。抱歉,是的。谢谢你,夏洛特。我只是重新阅读了答案,发现了这一点。太棒了,太棒了!可能重复的