Partager l'article ! Php : Simple fonction qui pour trouver les liens dans une chaine de caracteres: <?php function findHref($texte) { &nbs ...
<?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.