PREG_MATCH Help

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
techlinks
Forum Newbie
Posts: 18
Joined: Sun Dec 29, 2002 4:00 am

PREG_MATCH Help

Post by techlinks »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello Folks:

I'm pulling my hair out to accomplish the following task.  I have a routine that returns a string as follows:

Code: Select all

$mystring = 

"

<a class="resultsLink" href="http://www.somedomain.com/goforit?url=http://www.otherdomain.com?
args=www.mydomain.com" target="1" onmouseover='window.status="";return true;' onmouseout='window.status=" ";return true;'

";
I need to extract everything betweeen:

(a) The beginning <a class="resultsLink"
and
(b) The final quote

Essentially, I'm trying to extract the href in the string above

Can someone recommend the PREG_MATCH call I need to make? Once I get the URL, I will call a subsequent fopen() call on it.

But I can't move forward because newbie me can't even extract the URL from $mystring

Hoping someone can guide me; unfortunately, the tutorials aren't helping me.

Thanks everyone.

Lester


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

firstly, your string contains non-escaped double quotes :P which will give you a parsing error.

You want to look into a regex like..

Code: Select all

preg_match('/.+?href="(.+?)".+?/ism', $test, $matches);
print_r($matches);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
techlinks
Forum Newbie
Posts: 18
Joined: Sun Dec 29, 2002 4:00 am

Post by techlinks »

Hello Scottaay

Your code works. Thanks. But I don't particularly understand why your suggested expression works.

For instance, what I really need is to capture the following URL (from my example)

http://www.somedomain.com/goforit?url=h ... domain.com

But your expression says the following:

$pattern = '/.+?href="(.+?)".+?/ism'

What is the portion:

+?/ism

...doing or helping in my example? I'm not sure what it does. I would have figured that you wanted to end the pattern at the closing quote and space after the end of the URL.

Hope you don't mind the questions. I'm trying to learn this. I will write a mini tutorial when done so I can show others what I learned from your responses.

Thank you very much.

Lester
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

In simple terms it means all characters leading up to the first "thing" following it.
vapoorize
Forum Newbie
Posts: 22
Joined: Mon Dec 17, 2007 5:35 pm

Post by vapoorize »

Essentially, I'm trying to extract the href in the string above
match just the href attribute from any string:
~(?<=href=['"])[^'"]+(?=['"])~

where ~ is the delimiter

tested:
http://nancywalshee03.freehostia.com/re ... d=iv2t0rtp


Press FAQ on the tested link to see some nice tutorials :)
Post Reply