Jump to content

Recommended Posts

Ok so i've used curl to retrieve a website, but i now want to find (and store in a variable so i can do some math with that number) a specific number that is on the i know is on the wesbite and i know where on the website it is but i dont know what the number will be.

 

So i want to get the number of search results from a specific google search, this number obvs changes depending on what search word you use. So how would i go about doing this? This is my curl code:

 

$url = 'http://www.google.ca/search?q=dan';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$html = curl_exec($curl);
curl_close($curl);

 

Any help would be great!

Link to comment
https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/
Share on other sites

You find the (Results ) and grab everything between there. You can then split/explode it at "about" and viola you have your number.

 

Since I am not good at regex, here is an alternative way:

 

<?php
$string = "some site that has (Results 1-10 of about 50) ok";

$string = explode("(Results", $string);
$string = explode("of about", $string[1]);
$string = explode(")", $string[1]);

$number = $string[0];

echo $number;
?>

 

Given that there is no html code in that phrase on the site, it should work.

Something like, untested but it should work...

 


<?php

$str = file_get_contents ( 'http://www.google.com/search?q=phpfreaks' );

$regex = '#<b>(\d+)</b> - <b>(\d+)</b> of about <b>([0-9\,]+)</b>#i';

if ( preg_match ( $regex, $str, $out ) > 0 )
{
print_r ( $out );
}

?>

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.