RE: [PHP] Regular Expressions and urls

From: Date: Wed, 10 Oct 2001 19:23:43 +0000
Subject: RE: [PHP] Regular Expressions and urls
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-70716@lists.php.net to get a copy of this message
<? function make_url($txt){ return preg_replace("~((ftp://)|(http://)|(www))[^\]]*~", "<a href=\"\\0\" target=\"_blank\">\\0</a>",$txt); } $text0="[http://www.geocities.com/Heartland/5792/]"; $text1="[http://www.iidc.indiana.edu/~irca/]"; echo make_url($text0)."<br>"; echo make_url($text1)."<br>"; ?> How about like above ? -----Original Message----- From: Rob Gambit [mailto:robgambit@hotmail.com] Sent: Wednesday, October 10, 2001 4:55 PM To: php-general@lists.php.net Subject: [PHP] Regular Expressions and urls I am trying to find a regex to relace a url in text with the <a href=... to make it a link. The urls in the text are sometimes enclosed in () or [] Here is what I have now: function make_url($txt){ return eregi_replace("((ftp://)|(http://)|(www))(([a-z0-9.\-])*)(@)?(([a-z0-9\.])*) (([:/])?)((([a-z0-9]*)(@?)/*)+([.a-z]{2,4}){1})?((\??[a-z0-9\=]*)?)((\&[a-z0 -9\=]*)?)(([\.a-z]){2,4})?", "<a href=\"\\0\" target=\"_blank\">\\0</a>",$txt); } This works, except in some cases, like the following urls, the end path is left off. [http://www.geocities.com/Heartland/5792/] [http://www.iidc.indiana.edu/~irca/] Does anyone have something that will work with all urls? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#70716) next »