如何使用 php 链接字符串中的 url? [英] How do I linkify urls in a string with php?

查看:18
本文介绍了如何使用 php 链接字符串中的 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:

"Look on http://www.google.com".

我需要将其转换为:

查看 http://www.google.com"

原始字符串可以有 1 个以上的 URL 字符串.

The original string can have more than 1 URL string.

如何在 php 中执行此操作?

How do I do this in php?

谢谢

推荐答案

您可以使用以下内容:

$string = "Look on http://www.google.com";
$string = preg_replace(
              "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
              "<a href="\0">\0</a>", 
              $string);

PHP 版本 <5.3 (ereg_replace) 否则 (preg_replace)

这篇关于如何使用 php 链接字符串中的 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆