Page 1 of 1

PREG_MATCH Help

Posted: Fri Dec 21, 2007 12:38 am
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]

Posted: Fri Dec 21, 2007 1:23 am
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);

Posted: Sat Dec 22, 2007 6:12 pm
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

Posted: Sun Dec 23, 2007 9:55 am
by feyd
In simple terms it means all characters leading up to the first "thing" following it.

Posted: Mon Jan 07, 2008 7:01 pm
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 :)