多个域的 preg_replace 和数组
我有一个像这样的数组:
$array = array('domain1.com','domain2.net','domain3.org');
有什么方法可以仅将这些域替换为带有 preg_replace 的链接吗?
目前有这个小功能,但解析所有域:
function insert_referer($text){
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = substr($ret, 1);
return $ret;
}
i have an array like this:
$array = array('domain1.com','domain2.net','domain3.org');
any way to replace only these domains into links with preg_replace?
currently have this little function, but parses all the domains:
function insert_referer($text){
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = substr($ret, 1);
return $ret;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码做了你想要的:
我没有测试代码本身,所以它可能(我不认为它会,但有可能)包含拼写错误,但我测试了正则表达式本身,它工作得很好。
This code does what you wanted:
I didn't test the code itself so it may (I don't think it would but it's possible) contain typos, but I tested the regexp itself and it works perfectly.