Drezard Posted April 15, 2009 Share Posted April 15, 2009 Guys, Im trying to make a script that will create a list of our competitors with a couple of details from the local phone book website. Im using CURL to pull the data down (and as you can see, if you run it, it should work), it then dumps all of the HTML into $buffer. I then want to know (just for this example, then I'll build on it), how many of the lines contain the string "phoneNumber". It should be ATLEAST 5 - 10. So far $count only prints 1 at the end. Any ideas why? Script: #!/usr/bin/php -q <?php $url = "http://www.yellowpages.com.au/search/postSearchEntry.do?clueType=0&clue=computers&locationClue=Mordialloc%2C+VIC&x=0&y=0"; $chandle = curl_init(); curl_setopt($chandle, CURLOPT_URL, $url); curl_setopt($chandle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($chandle, CURLOPT_RETURNTRANSFER, 1); $buffer = curl_exec($chandle); curl_close($chandle); $array = explode("\r", $buffer); $size = sizeof($array); $i = 0; $count = 0; while ($i < $size) { $regex = preg_match("/phoneNumber/", $array[$i]); if ($regex == 1) { $count++; } $i++; } print $count . "\n"; ?> Thanks, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/154144-incorrect-return/ Share on other sites More sharing options...
premiso Posted April 15, 2009 Share Posted April 15, 2009 For something as simple as phonenumber use stristr to test. Regex is not needed for something that simple. Quote Link to comment https://forums.phpfreaks.com/topic/154144-incorrect-return/#findComment-810287 Share on other sites More sharing options...
Drezard Posted April 15, 2009 Author Share Posted April 15, 2009 Change the url to: $url = "http://www.yellowpages.com.au/search/postSearchEntry.do?clueType=0&clue=computer+hardware&locationClue=Mordialloc%2C+VIC&x=0&y=0"; What if I wanted to do it using Regex tho? Daniel Quote Link to comment https://forums.phpfreaks.com/topic/154144-incorrect-return/#findComment-810299 Share on other sites More sharing options...
darkfreaks Posted April 15, 2009 Share Posted April 15, 2009 if your looking to match a number in CURL with regex could try: http://developers.curl.com/userdocs/docs/en/dguide/regular-expressions.html Quote Link to comment https://forums.phpfreaks.com/topic/154144-incorrect-return/#findComment-810307 Share on other sites More sharing options...
premiso Posted April 15, 2009 Share Posted April 15, 2009 The point I am making is that you just want to count the number of times phoneNumber appears right? This is really not a job for regex, regex is more for finding a matches that you do not know, like if you wanted the actual phone number, you would use Regex for that. If that is what you want or your end goal is you need to say that. Because if not, regex is pointless for this and I doubt it can be done. <?php $url = "http://www.yellowpages.com.au/search/postSearchEntry.do?clueType=0&clue=computer+hardware&locationClue=Mordialloc%2C+VIC&x=0&y=0"; $chandle = curl_init(); curl_setopt($chandle, CURLOPT_URL, $url); curl_setopt($chandle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($chandle, CURLOPT_RETURNTRANSFER, 1); $buffer = curl_exec($chandle); curl_close($chandle); $occurances = substr_count($buffer, "phoneNumber"); print $occurances . "\n"; ?> As you can see substr_count can count the number of occurrences much easier than regex. But as I said, if you would like to grab the actual phone numbers, then say so. As what you want was too simple for regex. Quote Link to comment https://forums.phpfreaks.com/topic/154144-incorrect-return/#findComment-810315 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.