如何使用php链接字符串中的url? [英] How to linkify urls in a string with php?
本文介绍了如何使用php链接字符串中的url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下字符串,其模式如下:
I have the following strings having pattern which given below:
1)click on http://www.google.com
2) click onhttp://www.google.com".
3) click on www.google.com
4) click onwww.google.com
5) click on www.google.com#xyz
6) click onwww.google.com#xyz
我需要使用正则表达式将所有这些字符串转换为以下方式:
I need to convert all these string to below manner with using of regular expression:
click on <a href="http://www.google.com" target="_blank">http://www.google.com</a>
click onhttp://www.google.com
click on <a href="http://www.google.com" target="_blank">www.google.com</a>
click onwww.google.com
click on <a href="http://www.google.com#xyz" target="_blank">www.google.com#xyz</a>
click onwww.google.com
推荐答案
function linkify( $string ) {
return preg_replace( "~\bhttp(s)?://[^<>[:space:]]+[[:alnum:]/]\b~", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $string);
}
这篇关于如何使用php链接字符串中的url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文