SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: REG EX + cURL

  1. #1
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question REG EX + cURL

    I just want to share this snippet.

    Code:
    <?
    	$request_url ='http://www.qualitycodes.com';
     
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $request_url);	// The url to get links from
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);	// We want to get the respone
    	$result = curl_exec($ch);
     
    	$regex='|<a.*?href="(.*?)"|';
    	preg_match_all($regex,$result,$parts);
    	$links=$parts[1];
    	foreach($links as $link){
    		echo $link."<br>";
    	}
    	curl_close($ch);
     ?>
    What it does is finds all links in the href attribute of anchor tags of a given page url.
    So How can I get, the text in between a paragraph tag with a class named title?

    <p class="title">THE QUICK BROWN FOX</p>

    so for this example I just want to have THE QUICK BROWN FOX!

    Thanks for all!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    $regex='|<a.*?href="(.*?)"|';
    =>
    $regex='|<p[^>]*?class="title".*?>(.*?)</p>|';

    EDIT: I'm fairly sure that's wrong, and you'll have to do some post-regex filtering, because of how *? works, but give it a whack.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks StarLion, I did a few test and it worked actually....
    Thanks man, I probably sit to this regex thingy.... it's all Latin to me.

    For now, I will dig in to this example of yours. Thanks again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •