Re: For the RegExps gurus!

From: Date: Thu, 06 Sep 2001 18:15:08 +0000
Subject: Re: For the RegExps gurus!
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.general 
Request: Send a blank email to php-general+get-66255@lists.php.net to get a copy of this message
And here is version 5.2! I optimized the search patterns. I found out that there was only a very limited scope of URL's that was matched... So I pretty much fixed that now (see the example). Repeating elements from the patterns have been extracted and put in an array, so that you can easily see the diff between the two patterns. Also it's a bit easier to read this way. Also note the adjusted test-string $mem. There are some more realistic URL's in it than before. Just to see that they can realy be matched. --- PHP code --- <PRE> <?php $mem = "Hello world! http://www.helloworld.com/index.html\n Hello world! ftp://ftp-system.server.com?name=Renze%20Munnik\n Hello world! www.helloworld.com this-is/not=a%link.\n Hello world! www.helloworld.com/dynamic-html/main.php?and=some%20Extra"; /* --- RE version 5.2 --- */ /* Sub patterns: */ $sub = array ("(ht|f)tp://", /* protocols */ "(\w+|-)+\.", /* first (required) part of URL */ "\w+[/?=\.%\-]*");/* rest of the URL */ /* Search-patterns: */ $pattern = array ("~\s+(($sub[0])+($sub[1])($sub[2])+)~m", "~\s+(($sub[0]){0}($sub[1])($sub[2])+)~m"); $replace = array (" <A href=\"$1\">$1</A>", " <A href=\"http://$1\">http://$1</A>"); $mem = preg_replace ($pattern, $replace, $mem); print ($mem); ?> </PRE> --- End of PHP code --- -- * R&zE: -- »»»»»»»»»»»»»»»»»»»»»»»» -- Renze Munnik -- DataLink BV -- -- E: renze@datalink.nl -- W: +31 23 5326162 -- F: +31 23 5322144 -- M: +31 6 21811143 -- -- Stationsplein 82 -- 2011 LM HAARLEM -- Netherlands -- -- http://www.datalink.nl -- ««««««««««««««««««««««««

« previous php.general (#66255) next »