Link to home
Start Free TrialLog in
Avatar of phpdotnet
phpdotnet

asked on

Match all links which have specified text ?

I'm trying to crawl a forum but I don't know how to match all the links which have the text "showthread.php". Can anyone tell me how to do that with preg_match please ? Thank you very much.
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Can you show us some of the html or text you are "crawling"?

bol
The code below should get the entire "a" tag. Is that what you need?

preg_match_all('@<a [^>]+showthread.php[^>]*>@', $html, $matches);
Avatar of phpdotnet
phpdotnet

ASKER

Thanks but when I tried with  this http://www.webhostingtalk.com/forumdisplay.php?f=68 then the result array is :

Array
(
    [0] => Array
        (
            [0] => <a href="showthread.php?goto=newpost&t=624761" title="Go to first unread post in thread 'Record Breaking Downtime by Newista (Some Logic Inc)'">
            [1] => <a href="showthread.php?p=4640078#post4640078">
            [2] => <a href="showthread.php?goto=newpost&t=623727" title="Go to first unread post in thread 'Understanding and Verifying Uptime Guarantees'">
            [3] => <a href="showthread.php?p=4637807#post4637807">
...

I think it would be difficult but are there any way for me to echo the file name in each match ( eg this array is showthread.php ) and the vars after the file name to be an array where keys are the requested variables ( p, goto, t ... ) and values are  the strings to request ? ( something like this :

showthread.php?goto=newpost&t=624761

Array
(
      [0] => Array (
                  [0] => Array (
                        [0] => "showthread.php"
                        [1] => Array (
                              [goto] => "newpost"
                              [t] => "624761"
                        }      
            )
      )
)
SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks but when I tried your code, the output is :

Warning: explode() [function.explode]: Empty delimiter. in C:\xampp\htdocs\1.php on line 14

where line 14 is the

$temp_param2[]=explode($temp_param1[$pair],'=');

and the $temp_param1 is :

Array
(
    [0] => &
)

So I edit the code into :

$a = preg_replace('/showthread.php\?(.*)/','$1','showthread.php?goto=newpost&t=624761');
$b=explode('&',$a);
foreach ($b as $c) {
    $d=explode('=',$c);
      print_r($d);
}

And the result is :

Array
(
    [0] => goto
    [1] => newpost
)
Array
(
    [0] => t
    [1] => 624761
)

But are there any way for me to get this array into :

Array
(
    [goto] => "newpost"
    [t] => "624761"
)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Seems that MasonWolf's text is exactly what you were looking for.

MasonWolf,
Just looking at your code I think will make lots of use from it
fibo,

Always appreciate a compliment! Hope it helps you down the road.
I'm sorry for being careless like that. MasonWolf's answer is great and thank you very much.