Jump to content

Finding a specific word on a website


Dan1234

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 );
}

?>

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.