1,105,242 Community Members

cURL and regex question

Member Avatar
100110010110100
Newbie Poster
2 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have tried parsing a website with curl and then searching it with regular expressions, but I have not been able to perform a regex search. Even if I try parsing news.google.com and then search for any non digit, the code returns that there is no match.

If I run a regex search on a simple inputted string, on the other hand, it does find a match with no problem.

<?

//source website
$source = "http://news.google.com/?topic=w";

//function to parse with cURL
function openWithCURL($url){
 	$curl_handle=curl_init();
	curl_setopt($curl_handle,CURLOPT_URL, $url);
	$result = curl_exec($curl_handle);
	curl_close($curl_handle);
}


// parse website
$parsedsite = openWithCURL($source);

//Regular Expression to search for any single non digit (made it very inclusive so as to test out whether the regular expression works)

$regexp="/\D/";

if (preg_match($regexp, $parsedsite, $matches)) {
	echo "Found it!!";
	echo $matches[0];
}
	else {
	echo "Didn't find it!!";
}

?>

Any assistance in determining why I cannot seem to use regular expressions on information parsed from a webpage using curl would be greatly appreciated. Thanks.

Member Avatar
Insensus
Junior Poster
112 posts since Mar 2011
Reputation Points: 52 [?]
Q&As Helped to Solve: 46 [?]
Skill Endorsements: 0 [?]
 
0
 

Your openWithCURL function doesn't return anything.
I suppose you want

function openWithCURL($url){
 	$curl_handle=curl_init();
	curl_setopt($curl_handle,CURLOPT_URL, $url);
	$result = curl_exec($curl_handle);
	curl_close($curl_handle);
	return $result;
}
Member Avatar
100110010110100
Newbie Poster
2 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks, I had originally had the function just as you say, set to

return $results

. However, I just tried modifying the script to add the return value back in, and the regular expression still doesn't work.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: