Jump to content

Incorrect Return


Drezard

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/154144-incorrect-return/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/154144-incorrect-return/#findComment-810315
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.