note 42448 added to function.preg-replace

From: Date: Mon, 17 May 2004 08:56:35 +0000
Subject: note 42448 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-69704@lists.php.net to get a copy of this message
What an amateur I am. I pasted the wrong code into my note, this is the good one (sorry for the break-ups, the lines were too long): if ( preg_match( "/(http|ftp)+(s)?:(\/\/)((\w|\.|\-|_)+)(\/)?(\S+)?/i", $theText)) { $theText = preg_replace( "/(http|ftp)+(s)?:(\/\/)((\w|\.|\-|_)+)(\/)?(\S+)?/i", "<a href=\"$0\" target=\"_blank\">$4</a>", $theText); } else if ( preg_match( "/(www(.[\w]+))((\w|\.|\-|_)+)(\/)?(\S+)?/i", $theText) ) { $theText = preg_replace( "/(www(.[\w]+))((\w|\.|\-|_)+)(\/)?(\S+)?/i", "<a href=\"http://$0\" target=\"_blank\">$1$3</a>", $theText); }#endif If you need to replace several URL's inside the same function (the one above only replaces one instance of a URL), then you can use this one: $theText = preg_replace( "/[http|ftp+s?:\/\/]*(www(.[\w]+)) ((\w|\.|\-|_)+)(\/)?(\S+)?/i", "<a href=\"http://$1$3$5$6\" target=\"_blank\"> $1$3</a>", $theText ); ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42448 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42448&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42448&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69704) next »