Mardi 17 janvier 2012 2 17 /01 /Jan /2012 20:58

<?php

function findHref($texte)
{
    $order = array("\r\n", "\n", "\r");
    $texte = str_replace($order, " <br/> ", htmlspecialchars(stripslashes($texte)));   
   
    $texteExplode = explode(" ", $texte);
    foreach ($texteExplode as $key => $mot)
        if (preg_match_all("/(http|https):\/\/(\w{3}\.{1}|\w{0}\.{0})[A-Za-z0-9_\-\.]+(\.[A-Za-z]{2,4}(\/)?){1}(.)*/is", $mot, $matchs, PREG_SET_ORDER))
            $texteExplode[$key] = str_replace($matchs[0][0], "<a href='" . $matchs[0][0] . "' target='_blank'>" . $matchs[0][0] . "</a>", $texteExplode[$key]);
           
    return implode(" ", $texteExplode);
}

 

//Ex :

 

$str = "le blog le plus cool c'est http://agd243.over-blog.com/  ! \n Et merci a agd243 :-)";
echo findHref($str);

?>

A noter qui existe une autre fonction qui permet de trouver des liens d'image ou video Youtube | Megavideo automatiquement. Ici Php : Fonction pour trouver les liens dans une chaine de carateres.

 

Merci de commenter ce poste.

Par agd243 - Publié dans : Programmation - Communauté : Progghub
Ecrire un commentaire - Voir les 0 commentaires
Retour à l'accueil
 
Créer un blog gratuit sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus - Articles les plus commentés