Forum Moderators: coopster

Message Too Old, No Replies

preg match all() expression tweak

match all except one

         

sssweb

6:38 pm on Sep 26, 2006 (gmt 0)

10+ Year Member



I have the following code to pull URL's out of a text string:

preg_match_all('/(http:\/\/¦www\.)[^\s\'">]+/', $text, $matches);
$urls = $matches[0];

But I can't quite get how to tweak it to skip over one particular URL (mine!)

Can someone please insert 'mysite.com' appropriately in the above expression to pull all URL's EXCEPT mysite.com.

eelixduppy

3:06 am on Sep 27, 2006 (gmt 0)



I'm pretty sure the answer lies here:Lookahead and Lookbehind [regular-expressions.info]

Sorry for the lack of help, I'm still trying to make the pattern for this myself!

My regex skills are not fine-tuned at the moment. ;)

Good luck!

coopster

6:06 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Using an assertion would likely work. It might look something along these lines ...
$mysite = 'example.com'; 
$mysite = preg_quote($mysite, '/');
$pattern = "/((http:\/\/¦www\.)[^\s'\"\>\<]+)(?<!$mysite)[\s'\"\>\<]/i";
// $matches[1] has the full url then ...
print '<pre>'; print_r($matches[1]); print '</pre>';

Don't forget that the forum breaks the pipe symbol, you need to rekey it.