Page 1 of 2

A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 1:30 am
by soul_fly
Html Code:

<div class="result5464">
<a href="http://myserverserver.com/devnetwork" rel="nofollow"><img src="http://whatever.com/deevnet.jpg" alt="Dev Network"></a>
</div>

<div class="result3546">
<a href="http://myserverserver.com/soul_fly" rel="nofollow"><img src="http://whatever.com/soul.jpg" alt="soul_fly"></a>
</div>
My target is : http://myserverserver.com/devnetwork
http://myserverserver.com/soul_fly


I tried:
preg_match_all('/<a href=[\"']([^<]+)[\"']<\/a>/', $str, $matches);
echo $matches[0][0]."</br>";
echo $matches[0][1]."</br>";

Not worked. plz co-operate me.

In addition, I would like to know where can I get detailed explanation of those pattern like ([^<]+) or others.

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 8:26 am
by soul_fly
is there none to help a friend :(

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 10:41 am
by Mirge

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 10:53 am
by jackpf
Try this:

Code: Select all

preg_match_all('/href="([^"]+)"/i', $str, $matches);
 
print_r($matches);
And yeah, this is good too: http://www.addedbytes.com/cheat-sheets/ ... eat-sheet/

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 12:06 pm
by soul_fly
jackpf wrote:Try this:

Code: Select all

preg_match_all('/href="([^"]+)"/i', $str, $matches);
 
print_r($matches);

didn't work :(

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 12:32 pm
by jackpf
Works for me.

I get

Code: Select all

Array
(
    [0] => Array
        (
            [0] => href="http://myserverserver.com/devnetwork"
            [1] => href="http://myserverserver.com/soul_fly"
        )
 
    [1] => Array
        (
            [0] => http://myserverserver.com/devnetwork
            [1] => http://myserverserver.com/soul_fly
        )
 
)

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 12:37 pm
by soul_fly
jackpf wrote:Works for me.

I get

Code: Select all

Array
(
    [0] => Array
        (
            [0] => href="http://myserverserver.com/devnetwork"
            [1] => href="http://myserverserver.com/soul_fly"
        )
 
    [1] => Array
        (
            [0] => http://myserverserver.com/devnetwork
            [1] => http://myserverserver.com/soul_fly
        )
 
)

Could you plz check the syntax again:

preg_match_all('/href="([^"]+)"/i', $str, $matches);


It returns me blank page. sorry i'm still newbie in regex

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 12:40 pm
by jackpf
Did you copy my exact code and use print_r()??

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 12:51 pm
by soul_fly
jackpf wrote:Did you copy my exact code and use print_r()??
yup.

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 2:56 pm
by McInfo
The complete script:

Code: Select all

<?php
header('Content-Type: text/plain');
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('html_errors', false);
 
$str = <<<HTML
<div class="result5464">
<a href="http://myserverserver/devnetwork" rel="nofollow"><img src="http://whatever/devnet.jpg" alt="Dev Network"></a>
</div>
 
<div class="result3546">
<a href="http://myserverserver/soul_fly" rel="nofollow"><img src="http://whatever/soul.jpg" alt="soul_fly"></a>
</div>
HTML;
 
// jackpf's solution
preg_match_all('/href="([^"]+)"/i', $str, $matches);
echo 'Matches: ';
print_r($matches);
?>
Edit: This post was recovered from search engine cache.

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 3:35 pm
by jackpf
Well yeah, I just assumed he would put his own HTML in there...
Honestly.

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 3:50 pm
by McInfo
You know the old saying about what happens when you "ass+u+me"...

It's not obvious how the HTML is being acquired by the script or even if the HTML is making into the $str variable. I thought it would be better to just put it all together so there are fewer assumptions about what is being done on the other end of the conversation.

I added error reporting to the script so any oddities should show themselves, such as using the wrong variables or something (which is what I assume the problem is).

Edit: This post was recovered from search engine cache.

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 3:56 pm
by soul_fly
McInfo thanks for reply. Could I plz pm you?

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 4:04 pm
by soul_fly
ok lets be more practical. I was actually trying to grab urls from a webpage.

For example, I'm trying to grab URLs from http://forums.devnetwork.net (specifically which are in <a href"--------target----------")

So, I wrote this php codes:

Code: Select all

  
<? php
$ch = curl_init();            
$target = "http://forums.devnetwork.net";  
curl_setopt($ch, CURLOPT_URL, $target); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($ch);
preg_match_all('/href="([^"]+)"/i', $str, $matches);
print_r($matches);
?>
 
but output returns me
Array ( [0] => Array ( ) [1] => Array ( ) ) grrrr

Re: A preg_match_all problem. Looking for co-operation

Posted: Tue Sep 22, 2009 4:13 pm
by McInfo
After the

Code: Select all

<? php
which should be changed to

Code: Select all

<?php
add these two lines.

Code: Select all

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
Edit: This post was recovered from search engine cache.