Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Match all links which have specified text ?

Posted on 2007-08-02
9
Medium Priority
?
259 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:phpdotnet
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 19622582
Can you show us some of the html or text you are "crawling"?

bol
0
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19622716
The code below should get the entire "a" tag. Is that what you need?

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

Author Comment

by:phpdotnet
ID: 19623249
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"
                        }      
            )
      )
)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Assisted Solution

by:fibo
fibo earned 800 total points
ID: 19623799
If you get the rightmost part between the ? and the first ", and put it in $temp_query,
then you can get the array iof the parameters by

$temp_param1=array(); //reset it to be safe
$temp_param1=explode($temp_query, '&');
// we now need to explode each of these elements
$temp_param2=array(); // MUST be reset
foreach ($temp_param1 as $pair) {
  $temp_param2[]=explode($temp_param1[$pair], '=');
};
//the array $temp_param2 now holds all the required elements and values

Note: there is probably a way to combine explode into a single one, but I cannot experiment just now.
0
 

Author Comment

by:phpdotnet
ID: 19624611
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"
)
0
 
LVL 13

Accepted Solution

by:
MasonWolf earned 1200 total points
ID: 19625476
In the future, please tell us what you want when you first ask the question. Always remember to include what you have and what you want. If you had been more specific, you could have had your answer last night.

Here's a function that can get what you're looking for:

function getURLvars($html)
{
       preg_match_all('@<a [^>]+showthread.php[^>]*>@', $html, $matches);
       foreach($matches[0] AS $match)
      {
            preg_match('@\?[^\"]+\"@',$match,$vars);
            unset($php_vars);
            $vars = explode('&',$vars[0]);
            foreach($vars AS $var)
            {
                  $var = explode('=',str_replace(array('?','"','&'),array('','',''),$var));
                  $php_vars[$var[0]]=$var[1];
            }
            $final_vars[] = $php_vars;
      }
        return $final_vars;
}

Now, depending on how the initial html looks, you may need to first use:
"$html = str_replace('&amp;','&',$html);"
0
 
LVL 29

Expert Comment

by:fibo
ID: 19627147
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
0
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19627419
fibo,

Always appreciate a compliment! Hope it helps you down the road.
0
 

Author Comment

by:phpdotnet
ID: 19629542
I'm sorry for being careless like that. MasonWolf's answer is great and thank you very much.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question