使链接在文本块中可点击的最佳方法 [英] Best way to make links clickable in block of text

查看:59
本文介绍了使链接在文本块中可点击的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要:

Here is link: http://google.com
And http://example.com inside.
And another one at the very end: http://test.net

成为:

Here is link: <a href="http://google.com">http://google.com</a>
And <a href="http://example.com">http://example.com</a> inside.
And another one at the very end: <a href="http://test.net">http://test.net</a>

看起来像一个琐碎的任务,但是我找不到有效的PHP函数.你有什么主意吗?

Seems like a trivial task, but I cannot find a PHP function that works. Do you have any ideas?

function make_links_clickable($text){
    // ???
}

$text = 'Here is link: http://google.com
And http://example.com inside.
And another one at the very end: http://test.net';

echo make_links_clickable($text);

推荐答案

使用此功能(适用于ftp,http,ftp和https方案):

Use this (works with ftp, http, ftps and https schemes):

function make_links_clickable($text){
    return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1">$1</a>', $text);
}

这篇关于使链接在文本块中可点击的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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