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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.