We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,633 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

cURL and regex question

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.

2
Contributors
2
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
100110010110100
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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;
}
Insensus
Junior Poster
112 posts since Mar 2011
Reputation Points: 70
Solved Threads: 46
Skill Endorsements: 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.

100110010110100
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0627 seconds using 2.7MB