Thomisback Posted May 30, 2008 Share Posted May 30, 2008 Hi, I have made a PHP page which loads a page by using cURL. Now I want to extract a specific piece of text from the source but for some reason I can't do it. My code: <?php set_time_limit(0); $ch = curl_init("Link.example"); $usecookie = "cookie.txt"; curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie); curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie); $bestand = curl_exec($ch); if (curl_errno($ch)){ echo curl_error($ch); }else{ curl_close($ch); } ?> In the source page it says: <option value="5d5aedc1ab7ba7e79f8ccbdd9d031131">10 Minuten</option> Note: This value changes every time I tried something like: $data = explode("<option value=",$bestand); foreach ($data as $value) { echo $value; } But unfortunately this didn't work Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/ Share on other sites More sharing options...
rajivgonsalves Posted May 30, 2008 Share Posted May 30, 2008 try this <?php $strContent = <<< EOF <option value="5d5aedc1ab7ba7e79f8ccbdd9d031131">10 Minuten</option> EOF; preg_match("#value=\"(.*?)\"#",$strContent,$arrMatches); print_r($arrMatches[1]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553512 Share on other sites More sharing options...
Thomisback Posted May 30, 2008 Author Share Posted May 30, 2008 Thanks Unfortunately I noticed the value keeps changing so I'm forced to use the explode function :/ Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553518 Share on other sites More sharing options...
rajivgonsalves Posted May 30, 2008 Share Posted May 30, 2008 Yes it will pick up any value its regular expression change the value and see or use preg_match_all instead of preg_match Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553521 Share on other sites More sharing options...
Thomisback Posted May 30, 2008 Author Share Posted May 30, 2008 Hmm, I just tried it and the only thing it echoes is 5d5aedc1ab7ba7e79f8ccbdd9d031131 Not the "new" value I'm trying preg_match_all now Edit: Is $strContent supposed to be my cURL ouput page? Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553527 Share on other sites More sharing options...
Thomisback Posted May 30, 2008 Author Share Posted May 30, 2008 Isn't there a way to grab the text between <option value=" and ">10 Minuten</option> ? Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553645 Share on other sites More sharing options...
MadTechie Posted May 30, 2008 Share Posted May 30, 2008 Isn't there a way to grab the text between <option value=" and ">10 Minuten</option> ? yep <?php $data = '<option value="Hello Boy">10 Minuten</option>'; if (preg_match('%<option value="(.*?)">10 Minuten</option>%', $data, $regs)) { $result = $regs[1]; } else { $result = ""; } echo "found: $result" ?> Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553671 Share on other sites More sharing options...
Thomisback Posted May 30, 2008 Author Share Posted May 30, 2008 Wow thanks! ... I used it on my cURL source of a loaded page but it doesn't return anything My code: <?php set_time_limit(0); $ch = curl_init("X.PHP"); $usecookie = "cookie.txt"; curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie); curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie); $data = curl_exec($ch); if (curl_errno($ch)){ echo curl_error($ch); }else{ curl_close($ch); } if (preg_match('%<option value="(.*?)">10 Minuten</option>%', $data, $regs)) { $result = $regs[1]; } else { $result = ""; } echo "found: $result" ?> Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553680 Share on other sites More sharing options...
MadTechie Posted May 30, 2008 Share Posted May 30, 2008 post some sample data around the ">10 Minuten</option> or even try this if (preg_match('%<option value="(.*?)">10 Minuten</option>%sim', $data, $regs)) { note the sim Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553695 Share on other sites More sharing options...
Thomisback Posted May 30, 2008 Author Share Posted May 30, 2008 Thanks, when I use both your ways on '<option value="Hello Boy">10 Minuten</option>' It works fine, but when I try to run it onto my cURL output it doesn't return anything :S Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553699 Share on other sites More sharing options...
Thomisback Posted May 30, 2008 Author Share Posted May 30, 2008 Hmm I "fixed" it now, I first write it to a .txt file and then search it. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553708 Share on other sites More sharing options...
GingerRobot Posted May 30, 2008 Share Posted May 30, 2008 Well, if you hadn't set any other cURL options then $data wouldn't have contained the returned string, it would have just been output to the browser. You need to set RETURN_TRANSFER to true if you wish to work with the returned data rather than just outputing it straight away. curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); There's no reason why you should have to write it to a text file. I can only assume that in doing that you added the above option? Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553722 Share on other sites More sharing options...
Thomisback Posted May 30, 2008 Author Share Posted May 30, 2008 Ah thank you that was the line I was looking for! Thanks a lot for that! Quote Link to comment https://forums.phpfreaks.com/topic/107997-solved-exploding-curl-source/#findComment-553740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.