php plaintext,Convert plain text URLs into HTML hyperlinks in PHP

Here is an other solution, This will catch all http/https/www and convert to clickable links.

$url = '~(?:(https?)://([^\s

$string = preg_replace($url, '$0', $string);

echo $string;

Alternatively for just catching http/https then use the code below.

$url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';

$string= preg_replace($url, '$0', $string);

echo $string;

EDIT:

The script below will catch all url types and convert them to clickable links.

$url = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';

$string = preg_replace($url, '$0', $string);

echo $string;

New update, If you're having the string strip the (s) then use the below code block, Thanks to @AndrewEllis for pointing this out.

$url = '@(http(s)?)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';

$string = preg_replace($url, '$0', $string);

echo $string;

Here's a very simple solution for the URL not displaying correctly.

$email = 'email@email.com';

$string = $email;

echo $string;

It is a very simple fix but you will have to modify it for your own purpose.

I've provided multiple answers as some servers are setup differently, so one answer may work for some but not for others, but I hope the answer(s) work for you and if not then let me know and hopefully I can come up with another solution.

There are multiple scripts as some PHP files require different scripts also some servers are setup differently, Plus each have different requirements, Some want just HTTP/S, some want WWW and some want FTP/S, Each one will work depending on how the users own scripts are setup, I provided some text with each one with what they do.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值