Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2010, 08:17 PM   PM User | #1
Jinxy
New Coder

 
Join Date: Feb 2007
Posts: 86
Thanks: 0
Thanked 2 Times in 2 Posts
Jinxy has a little shameless behaviour in the past
preg_replace and regex help - Please

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?????">


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

Last edited by Jinxy; 01-09-2010 at 01:20 AM..
Jinxy is offline   Reply With Quote
Old 01-09-2010, 03:06 AM   PM User | #2
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
Code:
/(<a href=")(\/search\.php\?.+?)(">)(.+?)(<\/a>)/
Untested. Maybe something like that...
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Web Conversion Software on Facebook !! :)
chump2877 is offline   Reply With Quote
Old 01-09-2010, 04:40 AM   PM User | #3
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
'%<a [^>]+href="(?P<url>/search\.php\?[^"]+)"[^>*]*>(?P<text>[^< ]+)</a>%si'
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-09-2010, 02:55 PM   PM User | #4
Jinxy
New Coder

 
Join Date: Feb 2007
Posts: 86
Thanks: 0
Thanked 2 Times in 2 Posts
Jinxy has a little shameless behaviour in the past
Thanks guys!
Jinxy is offline   Reply With Quote
Old 01-09-2010, 09:02 PM   PM User | #5
PHP6
Regular Coder

 
PHP6's Avatar
 
Join Date: Aug 2008
Location: Czech Republic
Posts: 234
Thanks: 17
Thanked 34 Times in 33 Posts
PHP6 is on a distinguished road
All examples above are correct but I will suggest to make it more flexible, sine you could have three possible URLs on the page:
Code:
<a something href='/search.php?variables' something>
<a something href="/search.php?variables" something>
<a something href=/search.php?variables something>
PHP Code:
LINK => preg_match('#<a[^>]+href=(?:"|\'|)(/search\.php\?[^"\'\s]+)#'$data$values);
TEXT => preg_match('#<a[^>]+href=(?:"|\'|)/search\.php\?[^>]+>([^<]+)#'$data$values); 

Last edited by PHP6; 01-09-2010 at 09:04 PM..
PHP6 is offline   Reply With Quote
Old 01-09-2010, 09:18 PM   PM User | #6
Jinxy
New Coder

 
Join Date: Feb 2007
Posts: 86
Thanks: 0
Thanked 2 Times in 2 Posts
Jinxy has a little shameless behaviour in the past
Thanks PHP6! That works great!
Jinxy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:38 PM.


Advertisement
Log in to turn off these ads.