【问题标题】:PHP - Change text URL to real URL in string [duplicate]PHP - 将文本 URL 更改为字符串中的真实 URL [重复]
【发布时间】:2014-04-06 03:15:08
【问题描述】:

我有字符串:

$string1 = 'Hi John. Please, go to my website http://www.google.com :)';
$string2 = 'See this :P www.imgurl.com/abcd.jpg';

如何将此字符串转换为:

$string1 = 'Hi John. Please, go to my website <a href="http://www.google.com">http://www.google.com</a> :)';
$string2 = 'See this :P <img src="http://www.imgurl.com/abcd.jpg" alt ="" />';

我不知道;(

【问题讨论】:

    标签: php regex


    【解决方案1】:

    最好的方法是使用正则表达式将您的 URL 替换为 &lt;a&gt; 标签或 &lt;img&gt; 标签。棘手的部分将是确定它是图像还是普通 URL。

    在此处阅读有关 PHP preg_replace 函数的信息:http://de2.php.net/preg_replace

    这里是匹配 URL 的正则表达式列表:http://regexlib.com/Search.aspx?k=url&AspxAutoDetectCookieSupport=1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多