2009-10-19 4 views
0

J'ai cette chatbox PHP.PHP: STR remplacer par lien

Si je tapais un lien dans la boîte de dialogue, il ne l'afficherait pas comme un lien. Comment puis-je utiliser STR remplacer pour ce faire?

Il devrait répondre à des choses comme 'http' 'http: //' '.com' '.nl' 'www' 'www.' ....

Mon autre STR remplacer les lignes ressemblent à celles-ci:

$bericht = str_replace ("STRING1","STRINGREPLACEMENT1",$bericht); 

Quelqu'un?

Répondre

1

Hey! Essayez ce code (trouvé sur php.net quelque part):

function format_urldetect($text) 
{ 

    $tag = " rel=\"nofollow\""; 
    // 
    // First, look for strings beginning with http:// that AREN't preceded by an <a href tag 
    // 
    $text = preg_replace("/(?<!<a href=(\"|'))((http|ftp|http)+(s)?:\/\/[^<>\s]+[\w])/i", "<a target=\"_new\" class=\"httplink\" href=\"\\0\"" . $tag . ">\\0</a>", $text); 
    // 
    // Second, look for strings with casual urls (www.something.com...) and make sure they don't have a href tag OR a http:// in front, 
    // since that would have been caught in the previous step. 
    // 
    $text = preg_replace("/(?<!<a href=(\"|')http:\/\/)(?<!http:\/\/)((www)\.[^<>\s]+[\w])/i", "<a target=\"_new\" class=\"httplink\" href=\"http://\\0\"" . $tag . ">\\0</a>", $te 
xt); 
    $text = preg_replace("/(?<!<a href=(\"|')https:\/\/)(?<!http:\/\/)((www)\.[^<>\s]+[\w])/i", "<a target=\"_new\" class=\"httplink\" href=\"http://\\0\"" . $tag . ">\\0</a>", $t 
ext); 
    return $text; 
} 

Uhm, indentation brisée. essayez ceci http://triop.se/code.txt

+0

Merci, cela me met sur la bonne voie. –

+0

J'ai essayé, ça marche! Merci. –

0

Vous devez utiliser regex à la place. regarder preg_replace

$regex = '`(\s|\A)(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)`ism'; 
$replace = '$1<a href="$2://$3" target="_blank">$2://$3</a>$4' 

$buffer = preg_replace($regex,$replace,$buffer);