fonction php de détection de liens ?

WRInaute passionné
Bonjour,

Je cherche une fonction php qui détecte les liens dans les textes que je lui fournis et les rend actif.

C'est-à-dire que si dans le texte il y a un -http://www.example.com ou un -moi@monfai.com, elle les détecte et les encadre des balises qu'il faut pour qu'ils soiient cliquables (en protégeant si possible les e-mails).

Bref, rien de bien méchant, mais comme ça existe déjà, inutile de réinventer la roue. 8)

fab
 
WRInaute passionné
Ca doit être un truc comme ca :

eregi_replace("http://www.example.com","<a href='URL'>\\1</a>","TON TEXTE");
 
WRInaute accro
Code:
<?php
// Function converts plain-text email addresses and URLs to hyber links
function autoLink($str, $max = 1000) {

	// URL - http/https/ftp - larger than $max
	$pattern[0] = "/(?<!=\"|=)(http|https|ftp):\/\/([\S]{".$max."})([\S]{0,})/";
	$replace[0] = '<a href="$1://$2$3" title="$1://$2$3">$1://$2 ...</a>&nbsp;&nbsp;<a href="$1://$2$3" title="'.NAV_OPEN_NEW_WINDOW.': $1://$2$3" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	// URL - http/https/ftp - smaller than $max
	$pattern[1] = "/(?<!=\"|=|>)(http|https|ftp):\/\/([^\s]*)/";
	$replace[1] = '<a href="$1://$2">$1://$2</a>&nbsp;&nbsp;<a href="$1://$2" title="'.NAV_OPEN_NEW_WINDOW.': $1://$2" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	// URL - www - larger than $max
	$pattern[2] = "/(?<!=\"|=|http:\/\/)\b(w){3}\.([\S]{".$max."})([\S]{0,})\b/";
	$replace[2] = '<a href="http://www.$2$3" title="http://www.$2$3">www.$2...</a>&nbsp;&nbsp;<a href="http://www.$2$3" title="'.NAV_OPEN_NEW_WINDOW.': http://www.$2$3" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	// URL - www - smaller than $max
	$pattern[3] = "/(?<!=\"|=|http:\/\/|\">|>)\b(w){3}\.([^\s]*)\b/";
	$replace[3] = '<a href="http://www.$2">www.$2</a>&nbsp;&nbsp;<a href="http://www.$2" title="'.NAV_OPEN_NEW_WINDOW.': http://www.$2" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	// Email
	$pattern[4] = "/([A-Za-z0-9\+\-_]+)@([A-Za-z0-9-_]+)\.([A-Za-z]{2,4})([,.]?)/";
	$replace[4] = '<a href="mailto:$1@$2.$3">$1@$2.$3</a>$4';
	
	$str = preg_replace($pattern, $replace, $str);
	
	return $str;
}

?>

et si tu veux encoder les emails en Javascript, petite fonction en +:
Code:
function encodeEmail($email, $name = null) {

	$email = preg_replace("/\"/","\\\"",$email);

    if($name == null)
           $name = $email;

	$old = "document.write('<a href=\"mailto:$email\">$name</a>')";

	$output = "";

	for ($i=0; $i < strlen($old); $i++) {
	 $output = $output . '%' . bin2hex(substr($old,$i,1));
	 }

	$output = '<script type="text/javascript">eval(unescape(\''.$output.'\'))</script>';
	$output .= '<noscript><div>'.TXT_NEED_JAVASCRIPT_TO_VIEW_EMAIL.'</div></noscript>';
	return $output;
}

La fonction autoLink deviens donc:
Code:
// Function converts plain-text email addresses and URLs to hyber links
function autoLink($str, $max = 1000) {

	// URL - http/https/ftp - larger than $max
	$pattern[0] = "/(?<!=\"|=)(http|https|ftp):\/\/([\S]{".$max."})([\S]{0,})/";
	$replace[0] = '<a href="$1://$2$3" title="$1://$2$3">$1://$2 ...</a>&nbsp;&nbsp;<a href="$1://$2$3" title="'.NAV_OPEN_NEW_WINDOW.': $1://$2$3" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	// URL - http/https/ftp - smaller than $max
	$pattern[1] = "/(?<!=\"|=|>)(http|https|ftp):\/\/([^\s]*)/";
	$replace[1] = '<a href="$1://$2">$1://$2</a>&nbsp;&nbsp;<a href="$1://$2" title="'.NAV_OPEN_NEW_WINDOW.': $1://$2" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	// URL - www - larger than $max
	$pattern[2] = "/(?<!=\"|=|http:\/\/)\b(w){3}\.([\S]{".$max."})([\S]{0,})\b/";
	$replace[2] = '<a href="http://www.$2$3" title="http://www.$2$3">www.$2...</a>&nbsp;&nbsp;<a href="http://www.$2$3" title="'.NAV_OPEN_NEW_WINDOW.': http://www.$2$3" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	// URL - www - smaller than $max
	$pattern[3] = "/(?<!=\"|=|http:\/\/|\">|>)\b(w){3}\.([^\s]*)\b/";
	$replace[3] = '<a href="http://www.$2">www.$2</a>&nbsp;&nbsp;<a href="http://www.$2" title="'.NAV_OPEN_NEW_WINDOW.': http://www.$2" onclick="javascript:window.open(this.href);return false;"><img src="'.ABSOLUTE_URL.'images/new-window.gif" alt="Open in new window" /></a>';
	
	$str = preg_replace($pattern, $replace, $str);
	
	$emailPattern = "/([A-Za-z0-9\+\-_]+)@([A-Za-z0-9-_]+)\.([A-Za-z]{2,4})([,.]?)/";
	
	return preg_replace_callback(
		$emailPattern,
		create_function('$matches','return encodeEmail($matches[0]);'),
		$str);
}
 
Discussions similaires
Haut