A preg_match_all problem. Looking for co-operation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

A preg_match_all problem. Looking for co-operation

Post 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.
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: A preg_match_all problem. Looking for co-operation

Post by soul_fly »

is there none to help a friend :(
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: A preg_match_all problem. Looking for co-operation

Post by Mirge »

User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: A preg_match_all problem. Looking for co-operation

Post 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/
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: A preg_match_all problem. Looking for co-operation

Post by soul_fly »

jackpf wrote:Try this:

Code: Select all

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

didn't work :(
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: A preg_match_all problem. Looking for co-operation

Post 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
        )
 
)
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: A preg_match_all problem. Looking for co-operation

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: A preg_match_all problem. Looking for co-operation

Post by jackpf »

Did you copy my exact code and use print_r()??
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: A preg_match_all problem. Looking for co-operation

Post by soul_fly »

jackpf wrote:Did you copy my exact code and use print_r()??
yup.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: A preg_match_all problem. Looking for co-operation

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 12:30 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: A preg_match_all problem. Looking for co-operation

Post by jackpf »

Well yeah, I just assumed he would put his own HTML in there...
Honestly.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: A preg_match_all problem. Looking for co-operation

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 12:31 pm, edited 1 time in total.
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: A preg_match_all problem. Looking for co-operation

Post by soul_fly »

McInfo thanks for reply. Could I plz pm you?
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: A preg_match_all problem. Looking for co-operation

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: A preg_match_all problem. Looking for co-operation

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 12:32 pm, edited 1 time in total.
Post Reply