| |
|
Voir le sujet précédent :: Voir le sujet suivant
|
| Auteur |
Message |
| |
|
Fab le Fou WRInaute accro

Inscrit le: 02 Déc 2004 Messages: 1477 Localisation: Lorient
|
Posté le : Mer Jan 11, 2006 15:37 Sujet du message: fonction php de détection de liens ? |
|
|
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.
fab |
|
| |
|
 |
bozoleclown WRInaute passionné

Inscrit le: 24 Nov 2005 Messages: 766 Localisation: Paris, France
|
|
| |
|
 |
mr_go WRInaute accro

Inscrit le: 21 Sep 2005 Messages: 1944 Localisation: Sous le soleil
|
Posté le : Mer Jan 11, 2006 17:13 Sujet du message: fonction php de détection de liens ? |
|
|
Ca doit être un truc comme ca :
eregi_replace("http://www.example.com","<a href='URL'>\\1</a>","TON TEXTE"); |
|
| |
|
 |
spout WRInaute passionné

Inscrit le: 14 Mai 2003 Messages: 625 Localisation: Manhay (Belgique)
|
Posté le : Mer Jan 11, 2006 18:21 Sujet du message: fonction php de détection de liens ? |
|
|
| 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> <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> <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> <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> <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> <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> <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> <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> <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);
}
|
|
|
| |
|
 |
Fab le Fou WRInaute accro

Inscrit le: 02 Déc 2004 Messages: 1477 Localisation: Lorient
|
Posté le : Mer Jan 11, 2006 19:05 Sujet du message: fonction php de détection de liens ? |
|
|
Ok super, je vais tester la fonction de ce pas !
ça a l'air très complet, merci !
Fabrice |
|
| |
|
 |
| |
|
|
|
|
Autres sujets de discussion :
Définitions :
|
|