2010-11-13 7 views
0

i ont un tableau comme celui-ci:preg_replace et tableau pour plusieurs domaines

$array = array('domain1.com','domain2.net','domain3.org'); 

toute façon de ne remplacer que ces domaines en liens avec preg_replace?

ont actuellement cette petite fonction, mais parse tous les domaines:

   function insert_referer($text){ 
        $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text); 
        $ret = ' ' . $text; 
        $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); 
        $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret); 
        $ret = substr($ret, 1); 
        return $ret; 
       }   

Répondre

0

Ce code fait ce que vous vouliez:

 
$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>" 

Je n'ai pas testé le code lui-même, il peut, (I Je ne pense pas que ce soit possible, mais c'est possible) contenir des fautes de frappe, mais j'ai testé l'expression rationnelle elle-même et cela fonctionne parfaitement.

Questions connexes