Mardi 17 janvier 2012 2 17 /01 /Jan /2012 21:21

<?php

function findHttp($texte, $image = true, $video = true) {
    $order = array("\r\n", "\n", "\r");
    $texte = str_replace($order, " <br/> ", htmlspecialchars(stripslashes($texte)));   
   
    $patterns = array ("/(http|https):\/\/(\w{3}\.{1}|\w{0}\.{0})[A-Za-z0-9_\-\.]+(\.[A-Za-z]{2,4}\/){1}(.)+(\.jpg|\.jpeg|\.png|\.gif)/is",
                       "/(http|https):\/\/(\w{3}\.{1}|\w{0}\.{0})(megavideo\.com\/\?v=(.){8,}|youtube\.com\/watch\?v\=(.){11,}){1}/is",
                       "/(http|https):\/\/(\w{3}\.{1}|\w{0}\.{0})[A-Za-z0-9_\-\.]+(\.[A-Za-z]{2,4}(\/)?){1}(.)*/is");
    $texteExplode = explode(" ", $texte);
    foreach ($texteExplode as $key => $mot) {
        if (preg_match_all($patterns[0], $mot, $matchs, PREG_SET_ORDER)) {
            if ($image)
                $texteExplode[$key] = str_replace($matchs[0][0], "<img src='" . $matchs[0][0] . "' style='max-width:80%' />", $texteExplode[$key]);
            else
                $texteExplode[$key] = str_replace($matchs[0][0], "<a href='" . $matchs[0][0] . "' target='_blank'>" . $matchs[0][0] . "</a>", $texteExplode[$key]);
        }
        else if (preg_match_all($patterns[1], $mot, $matchs, PREG_SET_ORDER)) {
            if ($video) {
                $src = str_replace("/?v=", "/v/", str_replace("/watch?v=", "/v/", $matchs[0][0]));
                $texteExplode[$key] = str_replace($matchs[0][0], "<embed src='" . $src . "' type='application/x-shockwave-flash' width='100%' height='415px' allowscriptaccess='always' allowfullscreen='true'></embed>", $texteExplode[$key]);
            } else
                $texteExplode[$key] = str_replace($matchs[0][0], "<a href='" . $matchs[0][0] . "' target='_blank'>" . $matchs[0][0] . "</a>", $texteExplode[$key]);
        }
        else if (preg_match_all($patterns[2], $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 = "Lien http : http://agd243.over-blog.com/ \n Video : http://www.youtube.com/watch?v=ke8ZSi80j_0 \n Image : http://www.google.mu/images/nav_logo101.png \n Merci a agd243 ;-)";


echo findHttp($str, true, true);
?>

 

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