...

View Full Version : preg_replace and regex help - Please



Jinxy
01-08-2010, 09:17 PM
I wonder if someone would do me a small favor and help me with a regex problem? This code was written by somebody else but it works great for extracting link urls on a page. But I don't want it to look for ALL links, just the links that have this this beginning"

/search.php?

So actually the links would look like this

<a href="/search.php?????">



$source = preg_replace("/a[\s]+[^>]*?href[\s]?=[\s\"\']+"."(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/", urlencode($source), $source);

Thanks!

chump2877
01-09-2010, 04:06 AM
/(<a href=")(\/search\.php\?.+?)(">)(.+?)(<\/a>)/

Untested. Maybe something like that...

JAY6390
01-09-2010, 05:40 AM
'%<a [^>]+href="(?P<url>/search\.php\?[^"]+)"[^>*]*>(?P<text>[^< ]+)</a>%si'

Jinxy
01-09-2010, 03:55 PM
Thanks guys!

PHP6
01-09-2010, 10:02 PM
All examples above are correct but I will suggest to make it more flexible, sine you could have three possible URLs on the page:

<a something href='/search.php?variables' something>
<a something href="/search.php?variables" something>
<a something href=/search.php?variables something>


LINK => preg_match('#<a[^>]+href=(?:"|\'|)(/search\.php\?[^"\'\s]+)#', $data, $values);
TEXT => preg_match('#<a[^>]+href=(?:"|\'|)/search\.php\?[^>]+>([^<]+)#', $data, $values);

Jinxy
01-09-2010, 10:18 PM
Thanks PHP6! That works great! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum