Beginner’s guide to HTML parsing or web scrapping with PHP

What is Web Scraping?

According to Wikipedia, Web scraping means “Web scraping (web harvesting or web data extraction) is a computer software technique of extracting information from websites.”
It simply means that you can copy data from other websites to show or store it on your website. Lets say you want to create a currency exchange calcular but you don’t want to input currency rates everyday on the website because its tedious work. You can purchase an API that can cost you around 500 USD or more or you can simply parse the currency values from any website that offers the same service. Simple as that.

Lets start with our today’s project

I’d extract information from state bank of Pakistan’s website to obtain US Dollar, GB Pound, Japanese Yen and Euro to Pakistani Rupee exchange rates.. In PHP, We need a library called PHP Simple HTML DOM Parser for this specific purpose. So lets head over to the website and download the library.

Source Code

[php] <?php require_once("simpledom.php"); // Loading the Library
$sUrl = file_get_contents(""); //Enter the Webpage you want to parse
$sPageContent = new simple_html_dom();                       // Create New object

$sTable = $sPageContent->find("form",0)->find("table",22);   // We’d Parse the Table No 23 within first form

$sUsd   = $sTable->find("table",0)->find("td",1)->plaintext;  // Get value from td 2 within table 1 in plain text
$sUsd   =  substr($sUsd,strpos($sUsd," ")+1);                 // Elemenate "USD " from the result

$sGbp   = $sTable->find("table",1)->find("td",1)->plaintext;  // Rinse and Repeat
$sGbp   =  substr($sGbp,strpos($sGbp," ")+1);

$sJpy   = $sTable->find("table",2)->find("td",1)->plaintext;
$sJpy   =  substr($sJpy,strpos($sJpy," ")+1);

$sEur   = $sTable->find("table",3)->find("td",1)->plaintext;
$sEur   =  substr($sEur,strpos($sEur," ")+1);

print "USD : ".$sUsd." – GBP : ".$sGbp." – JPY : ".$sJpy." – EUR : ".$sEur; // Here is the output


Here are the currency rates as off 22 July, 2016.

USD : 104.8398 – GBP : 138.3047 – JPY : 0.9938 – EUR : 115.4916

These Values are in Pakistani rupee and we can do whatever we want to with them. Hope you liked this tutorial. Here’s detailed manual.

You can reach Waqas Yousaf through twitter @wiqi.

Read More
Waqas Yousaf October 23, 2016 1 Comment

5 reasons to never use Flash on your website

Back in the 2000’s, When Flash came out first time with its smooth animations and sound support, It amazed me. I learnt Flash animations so that I can make my website cooler (I know but It was early 2k) but now days I recommend my clients to tick off the flash off their project list for following reasons

Stability and performance issues

Flash can make low end machines crippling slow because it guzzles resources like nothing else. Remember the browser crashes when you tried loading it in the past? Yeah.

Poor Usability

Who would like to encounter a 3-minute intro before browsing the website? Newsflash: No one has that much time. It also uses some effects (Mouse Roll over) that touch devices can’t understand, and try navigating a website with flash navigation, It’s the easiest recipe to hate your life, and don’t even get me started on that horrid UI/UX.

Better Alternatives

Developer use different JS libraries (JQuery, the most popular) to have great visual effects and animations on your website without straining your system. You can embed a video with HTML5 without any Flash these days. How cool is that?

Apple don’t and wont support it.

Apple devices don’t support flash. You won’t like to sacrifice a huge chunk of visitors to have some funky animations on your website. Would you?

And Finally,

Its disaster for SEO

Its internet age and Everything comes down to the SEO aka Search Engine Optimization. Search engines don’t recognize it. Simple. Search Engines loves to parse content. While flash can provide them some meta tags but It really effect the search engine optimization of your website in negative way.

Read More
Waqas Yousaf October 21, 2016 1 Comment

Create banned words list for WP titles

Why do you need a banned word list for titles?

Running a blog with multiple authors is such a nightmare. You got to deal with many things and on the top of the all you also got to look for the content being posted on the blogs.

You can either reduce the access level of bloggers and read all the content first by yourself and then publish it but it would take like forever and it would only add another task to your responsibilities. Then there is this way, in which you can add words you want to ban on the website and WordPress simply won’t let the bloggers add the titles with the banned words itself. Problem Solved.

How Do I Access Functions.php?

You’ll have to add this tiny function and a hook in your functions.php file which is stored in your theme’s directory.



[php] function wpb_title_bws($sTitle)
$BannedWords = "abc,xyz,omg"; // Banned words seperated by comma.
$BannedWords = explode(",", $BannedWords);

global $post;
$sTitle = $post->post_title;

foreach($BannedWords as $BannedWord)
if (stristr($sTitle, $BannedWord))
wp_die( __(‘Error: Remove the banned word "’. $BannedWord .’" in post title first!’) );

add_action(‘publish_post’, ‘wpb_title_bws’, 10, 1);


You can reach Waqas Yousaf through twitter @wiqi.

Read More
Waqas Yousaf October 19, 2016 10 Comments

Generate PDF with PHP | PHP2PDF Tutorial

Why Generate PDF Files With PHP?

No site is completed with out generating PDF file reports today. There are plenty of open source free libraries to achieve this task but my personal favorite is FPDF class. I find it suitable for the task because of its simplicity, relatively smaller size and ease of use.

Just head to the to download a free copy of the library. Unzip it in your web directory and include “fpdf.php” and that’s about it.

Source Code

Below, You can find a pretty basic no bells and whistles code to make use of fpdf library and generate pdf file with php.

[php] <? require(‘fdpf/fpdf.php’); $ObjPdf = new FPDF(); $ObjPdf->AddPage();
$ObjPdf->SetFont(‘Arial’,’ B’,16);
$ObjPdf->Cell(30,20,’Please generate the pdf for me..’);


Lets say you have saved the code in your local webserver’s root directory with file name “pdf.php” and you have library in a folder named “fpdf”, then you’ll open pdf.php in your browser, it will generate a pdf file.

Happy experimenting.

You can reach Waqas Yousaf through twitter @wiqi.

Read More
Waqas Yousaf October 16, 2016 0 Comments

Increasing logged-in expiry time of your wordpress website

Why Increase Loggin-in Expiry Time

Sometimes while developing a wordpress website, you dont want wordpress to log you off automatically from the website you are working on or in an other case, you might want to increase log-in expiry timer for your website’s users. Here’s a tiny piece of code that you can copy and paste into your functions.php file to help achieve the goal.

Source Code

[php] add_filter( ‘auth_cookie_expiration’, ‘loggedin_for_10days’ );

function loggedin_for_10days( $expire )
return 864000; // 10 days in seconds


You can reach Waqas Yousaf through twitter @wiqi.

Read More
Waqas Yousaf October 14, 2016 1 Comment

Mega WordPress Cheat sheet for Developers

If you are into developing wordpress website, this cheat sheet is a must have.

Download Here

Courtesy of  makeawebsitehub

Read More
Waqas Yousaf October 13, 2016 0 Comments

Running wordpress shortcodes in widgets

WordPress bares to add shortcodes to widges, wordpress would tweet shortcodes just as regular text. Its good thing by security point of view but you can make short codes run in widgets. All you have to do is to add this little piece of code in functions.php

[php] add_filter(‘widget_text’, ‘do_shortcode’);

and thats about it. happy experimenting.

Read More
Waqas Yousaf October 13, 2016 0 Comments

Speed up your website with gzip compression

What is GZIP Compression

You need to enable GZIP compression on your website to reduce load time of your website. Sharp website development and aggressive search engine optimization (SEO) is vital for every website but adding heap of content takes it tool on your website’s viewership. Research shows that your website will leave your website if it made them more than 20 seconds. Modern website designs are incomplete without CSS, Java scripts, PHP and images but sometimes these things piles so much that they choke the bandwidth of the consumer and consumers leave your website disgruntled after your website loading lower than the speed of a snail.

Benefit of Enabling GZIP Compression

By enabling compression for your website, it’ll simply compress the items browser needs to download to load your website. It will reduce the webpage load by 50 to 70% and it will keep your customer or viewer keep stuck to your website.

How enable GZIP Compression?

If your website is running on apache webserver, you can enable compression for your website by just copying the following code and paste it in your .htaccess file (can be found in the www or public html directory of your website.


[php] #Enable Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
# For Olders Browsers Which Can’t Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Give the code time of 2-3 hours to propagate itself and then check upon your website if it has enabled or not.If code given above doesn’t work out, remove the above code and add the code given below instead.


[php] AddOutputFilterByType DEFLATE text/plain

AddOutputFilterByType DEFLATE text/html

AddOutputFilterByType DEFLATE text/xml

AddOutputFilterByType DEFLATE text/css

AddOutputFilterByType DEFLATE application/xml

AddOutputFilterByType DEFLATE application/xhtml+xml

AddOutputFilterByType DEFLATE application/rss+xml

AddOutputFilterByType DEFLATE application/javascript

AddOutputFilterByType DEFLATE application/x-javascript


You can reach Waqas Yousaf through twitter @wiqi.

Read More
Waqas Yousaf October 12, 2016 22 Comments

6 keyword tools for your website’s SEO and PPC success

Why keyword tools are important for website’s SEO

Getting your website trended for a specific keyword has become an art now a days. People are trying hard to get the ranks of their websites better and taking them on the first page of famous search engines like google and Bing. Getting your website filled with content is not hard but this task becomes tricky when you have to put some content on your website that is specific, relevant and that can take your website off to the first page. Researching right keywords has become much of a science today because of its importance. Today, I am going to write about some of the best keyword tools available online for free that you can use to generate a better list of keywords for the content of your website and for peak search engine optimization .


6. Soolve


Soolve is multichannel keyword hunter. It not only fetch keywords for google but also other famous plateforms like answers, yahoo, bing and youtube. Its real-time keyword generation feature is what it makes it stands out in other SEO research tools.



5. SEO Book Keyword Tool


SEO book keyword tool uses various channels to harness the keywords and generates you a list with more keywords not only from search engines but also from dictionary.


4. Keywordeye


Keyword is great tool to simplifying your keyword researching. It allows you 10 keyword searches a day. The good thing is their visualization tool which shows the important keywords for search engine optimization based on their importance.


3. WordStream

This tool allows you 30 keyword searches per day and its one of the bet tools I have come across when it comes to ppc and SEO. It suggests you a grouping for related keywords based to put them in your content.


2. Ubbersuggest

ubersuggestIt fetches keywords from google suggest function and gives you ideas from what users are actually searching for in real time.


1. Google Adwords keyword Tool

adwordsThe holy grail for SEO and PPC. Google’s official keyword tool. It shows you every bit of a stat for a keyword like competition, search volume and obviously, average cost for pay per click (PPC) ads. It would give you alterative keywords and relevant searches.

You can reach Waqas Yousaf through twitter @wiqi.

Read More
Waqas Yousaf October 11, 2016 10 Comments