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("http://www.sbp.org.pk/"); //Enter the Webpage you want to parse
$sPageContent = new simple_html_dom();                       // Create New object
$sPageContent->load($sUrl);

$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
?>

[/php]

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.

CAUTION: BACKUP YOUR WEBSITE BEFORE DOING ATTEMPTING CHANGES. I WONT BE RESPONSIBLE FOR ANY MISHAP.

Code

[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);
[/php]

 

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 fpdf.org 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->SetTextColor(0,0,0);
$ObjPdf->SetFont(‘Arial’,’ B’,16);
$ObjPdf->Cell(30,20,’Please generate the pdf for me..’);
$ObjPdf->Output();
?>
[/php]

Result

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
}
[/php]

 

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’);
[/php]

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/vnd.ms-fontobject
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
</IfModule>
[/php]

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
[/php]

 

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

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

seobook

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

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

PHP Date and Time Reference | Cheatsheet

Why Do I Need PHP Date And Time Refernce?

Need for a PHP date and time reference arose when I was working on a blog and I had to check for the date and time functions every now and then. Let me admit something here. Nobody is a perfect programmer. We usually need references and cheatsheets to get out word done quickly. PHP like every other maintained programming language has like thousands if not millions of function names and parameters.
I cannot remember all the functions and classes for a specific task. I simply needed an online reference for PHP date time functions so that I dont have to go snoop around like 30 pages to find one itsy bitsy function.

date() Function

date() month pretty much handy while you are dealing with date/time functionality in PHP. for example

[php]print date("d-m-Y");[/php]
Output:  05-07-2016

this function would print current date in simple day-month-year format as shown above.

[php]date("h:i:s")[/php]
Output:  10:37:58

and this function would print time in mundane hour:minutes:seconds format as given below

You can also add other details like day and am/pm as well.

[php]print date("l d-m-Y h:i A");[/php]
Output:  Tuesday 05-07-2016 10:44 AM

If you wanted to check if a date exists or not, you can use checkdate(‘m’,’d’,’Y’) function
It works something like this

[php]$check = (checkdate(20, 10, 1985)) ? "TRUE" : "FALSE";
print $check; [/php]
Output:  FALSE

It would print false as output because 20th doesnt exist. or does it?

date_create() Function

There’s a date_create() function to create an date object so date/time funcsionality.

[php]$date = date_create();
var_dump($date);
[/php]
Output:  object(DateTime)[1]
public 'date' => string '2016-07-02 09:06:20' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)

 

date_diff() Function

date_diff() is a function that gives out the interval between two given dates.

[php]

$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1,$date2);
var_dump($diff) ;
[/php]

Output:  object(DateInterval)[3]
public 'y' => int 0
public 'm' => int 8
public 'd' => int 27
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 0
public 'days' => int 272
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0

 

date_format() Function

date_format() is a function to format date as provided.

[php]$date=date_create("1999-07-30");
print date_format($date,"d-m-Y");[/php]
Output:  30-07-1999

 

time() Function

time() function gives you current epoch or unix time.

[php]print time();[/php]
Output:  1467707548

 

gmdate() Function

You can format the output in human reabible using gmdate() function

[php] $unixTime = time();
print gmdate(‘l h:i:s d-m-Y’, $unixTime);
[/php]
Output:  Tuesday 10:09:13 05-07-2016

 

strtotime Function

function returns relative time,string unix timestamp format.

[php]print strtotime("now");
print strtotime("12 December 2009");
print strtotime("+8 hours");
print strtotime("+1 week");
print strtotime("+1 week 3 days 7 hours 5 seconds");
print strtotime("next Friday");[/php]

 

Output:  1260572400
1467738028
1468314028
1468598433
1467928800

 

Relative Time Function

Here’s one script I have found on github to find relative time.

Just copy above above function and then you can use it in your program like

[php]print time2str(strtotime("10-june-2016"));[/php]

And the output would be something like this

Output:  4 weeks ago

 

You can reach Waqas Yousaf through twitter @wiqi.

Read More
Waqas Yousaf August 12, 2016 1 Comment