IPB

Bienvenue invité ( Connexion | Inscription )

[ En ligne ] · Standard · Linéaire+

> [php] expressions régulières

niko
post 06/07/2002 21:09
Message #1


Muggle
****

Groupe : Membres
Messages : 717
Inscrit le : 03/07/2002 23:00
Lieu : Toulouse
Membre no. 85



voilà je suis en train de coder un forum du style phpbb, vbulletin, ce que vous voulez pour l'intranet de mon IUT....

Je souhaite trasformer automatiquement les url des messages en leur équivalent en BB-Code, saufbiensûr si il s'agit d'url dans une balise bb-code justeemnt ...

J'ai fait ça :

CODE

$texte2 = eregi_replace("[^(\[url\])][url=http://(]http://([/url][^,;! \n\r\t]*)[^(\[/url\])]", "\[url\][url=http://\\2\]http://\\2\[/url][/url\]", $texte);
$texte2 = eregi_replace("[^(\[img\])][url=http://(]http://([/url][^,;! \n\r\t]*)[^(\[/img\])]", "\[url\][url=http://\\2\]http://\\2\[/url][/url\]", $texte2);
$texte2 = eregi_replace("[^(\[url=)][url=http://(]http://([/url][^,;! \n\r\t]*)[^(\].*\[/url\])]", "\[url\][url=http://\\2\]http://\\2\[/url][/url\]", $texte2);


les url dans les tags du bb-code ne sont pas transformées donc de ce coté là tout va bien ..... en revanche les url du style http://.... dans un message ne sont pas transformées en [ url ] http://... [/url][ /url ] comme je le souhaiterais et je ne comprends pas pourquoi :/

Quelqu'un peut m'aider ?????


--------------------
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
 
Reply to this topicStart new topicStart Poll
Réponse(s)
Peter Pan
post 11/07/2002 21:10
Message #2


Administrateur
*****

Groupe : Admin
Messages : 3,026
Inscrit le : 16/05/2002 23:00
Lieu : Paris
Membre no. 1



Tiens, voilà comment c'est fait dans phpBB 2 :
CODE
// pad it with a space so we can match things at the start of the 1st line.
$ret = " " . $text;

// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
// xxxx can only be alpha characters.
// yyyy is anything up to the first space, newline, or comma.
$ret = preg_replace("#([\n ])([a-z]+?)://([^,\t \n\r]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $ret);

// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
// yyyy contains either alphanum, "-", or "."
// zzzz is optional.. will contain everything up to the first space, newline, or comma.
// This is slightly restrictive - it's not going to match stuff like "forums.foo.com"
// This is to keep it from getting annoying and matching stuff that's not meant to be a link.
$ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,\t \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $ret);

// matches an email@domain type address at the start of a line, or after a space.
// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
$ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

// Remove our padding..
$ret = substr($ret, 1);

return($ret);
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Posts in this topic


Reply to this topicTopic OptionsStart new topic
1 utilisateur(s) sur ce sujet (1 invité(s) et 0 utilisateur(s) anonyme(s))
0 membre(s) :
 

Version bas débit Nous sommes le : : 05/07/2025 12:40