Php 用于多个域的preg_替换和数组

Php 用于多个域的preg_替换和数组,php,regex,preg-replace,Php,Regex,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', "

我有这样一个数组:

$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;
                }         
函数插入\u referer($text){
$text=preg_replace(“#(脚本|关于| applet | activex | chrome):#is',“\\1:”,$text);
$ret=''.$text;
$ret=preg\u replace(“#(^ |[\n])([\w]+?:/[\w\\\$%&~/.\-:=,?@\[\]+]*)是“,“\\1”,“$ret”);
$ret=preg_replace(“#(^|[\n])((www.ftp)\.[\w\\\$%&-/.\-]:=,?@\[\]+]*)是“,”\\1“,$ret);
$ret=substr($ret,1);
返回$ret;
}         

此代码满足您的要求:

$array = array('domain1.com','domain2.net','domain3.org'); $text = 'Some text including domain1.com/something and http://domain3.org'; echo preg_replace('/((?:https?:\/\/)?(?:' . implode('|', $array) . ')[-a-zA-Z0-9\._~:\/?#\[\]@\!\$&\'\(\)\*\+,;=]*)/', '\1', $text); // Outputs: // "Some text including <a href="domain1.com/something">domain1.com/something</a> and <a href="http://domain3.org">http://domain3.org</a>" $array=array('domain1.com'、'domain2.net'、'domain3.org'); $text='一些文本,包括domain1.com/something和http://domain3.org'; echo preg\u replace('/((?:https?:\/\/)(?:'.内爆('.'124;',$array)。)[-a-zA-Z0-9\.\u-:\/?\[\]@!\$&'(\)\*\+,;=]*/','\ 1',$text); //产出: //“某些文本包括和” 我没有测试代码本身,所以它可能(我不认为它会,但它是可能的)包含拼写错误,但我测试了regexp本身,它工作得非常完美