SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy cURL and preg_replace

    My goal here is to load a remote page (using cURL) then get ONLY the data between a <p> and </p> and turn it into an array. The page only has one <p></p> in it. The problem here is that it well... just won't work!
    heres my code:
    PHP Code:
          <?php
          $ch 
    curl_init();
          
    $timeout 5;
          
    curl_setopt ($chCURLOPT_URL'http://www.site.com/ex.php?var=ex');
          
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
          
    curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
          
    $file_contents curl_exec($ch);
          
    curl_close($ch);
          
    //$a='asdfasdf <p> whatever between tags </p> fdsgfg'; 
          
    $a $file_contents;
          
    $b=preg_replace("/<p[^>]*>(.+)<\/p>.*/i",'$1',$a);
          echo 
    $b;
         
    ?>
    When i preg_replace the original $a, the code outputs the 'asdfasdf' and 'whatever between tags' and thats it. When i use the preg_replace with the $file_contents, it doesn't really do anything at all (outputes the entire page). If there are alternate methods to get what i want accomplished, i would LOVE to hear about them.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should be using preg_match

    also use the s modifier so that . will match new lines as well

  3. #3
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working, now this thing is straight bad ***.

    Isn't it? Refresh the page


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
  •