PHP Parsing
Well I have been working on a new project that requires extensive PHP parsing. So as the first post about PHP, I decided to put up a little tutorial.
This tutorial will show you basic parsing of a web page to get one piece of data that is wanted.
This tutorial example will be getting the amount of search queries for when you google BncApps.
When you search, it gives you this url:
http://www.google.ca/search?hl=en&q=bncapps&btnG=Google+Search&meta=
This will be needed, remember this.
Lets start our php document by putting in the header and footer parts.
<?php //Our code will go here! ?>
So now lets grab everything from that search query, after
$Site = file_get_contents('http://www.google.ca/search?hl=en&q=bncapps&btnG=Google+Search&meta=');
This will download the entire html document from the search and store it in the string variable, Site.
Finish reading in the full article.
Now that we have the contents of the html file, now we just need to grab the part we want. This can easily be done with the explode function. Add these 2 lines below the previous added line.
'http://www.google.ca/search?hl=en&q=bncapps&btnG=Google+Search&meta='); $splitA = explode('of about < b>', $Site); $splitB = explode('</b> for ',$splitA[1]);
First it opens the document from the string we saved earlier, $Site and it will grab everything after the first value. If you click view source on Google and find where it has the search result value, you will notice that is the piece of data right before that is never changing. In the second part, splitB it is grabbing the part in between, which is our data.
Now just print it out:
echo $splitB[0];
It is stored in an array, without the 0 it will not work.
Here is the full code:
<?php $Site = file_get_contents('http://www.google.ca/search?hl=en&q=bncapps&btnG=Google+Search&meta='); $splitA = explode('of about < b>', $Site); $splitB = explode('</b> for ',$splitA[1]); echo $splitB[0]; ?>
And that's it, when it is run, it should give you the amount of search queries for "bncapps".
Due to the limitations of WordPress, I can not make the script run within the post.



