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.
The code below should get the entire "a" tag. Is that what you need?
preg_match_all('@<a [^>]+showthread.php[^>]*>@ ', $html, $matches);
preg_match_all('@<a [^>]+showthread.php[^>]*>@
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=464 0078#post4 640078">
[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=463 7807#post4 637807">
...
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=newpos t&t=624761
Array
(
[0] => Array (
[0] => Array (
[0] => "showthread.php"
[1] => Array (
[goto] => "newpost"
[t] => "624761"
}
)
)
)
Array
(
[0] => Array
(
[0] => <a href="showthread.php?goto=
[1] => <a href="showthread.php?p=464
[2] => <a href="showthread.php?goto=
[3] => <a href="showthread.php?p=463
...
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=newpos
Array
(
[0] => Array (
[0] => Array (
[0] => "showthread.php"
[1] => Array (
[goto] => "newpost"
[t] => "624761"
}
)
)
)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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($te mp_param1[ $pair],'=' );
and the $temp_param1 is :
Array
(
[0] => &
)
So I edit the code into :
$a = preg_replace('/showthread. php\?(.*)/ ','$1','sh owthread.p hp?goto=ne wpost&t=62 4761');
$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"
)
Warning: explode() [function.explode]: Empty delimiter. in C:\xampp\htdocs\1.php on line 14
where line 14 is the
$temp_param2[]=explode($te
and the $temp_param1 is :
Array
(
[0] => &
)
So I edit the code into :
$a = preg_replace('/showthread.
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
Always appreciate a compliment! Hope it helps you down the road.
ASKER
I'm sorry for being careless like that. MasonWolf's answer is great and thank you very much.
bol