Quantcast
Channel: Php Snippets Archives - BinaryTides
Browsing latest articles
Browse All 20 View Live

Php array of iso 639-1 language codes and names

Here is a php array containing the language codes and language names as specified by the iso 639-1 standard. Useful when . <?php /** ISO 639-1 Language Codes Useful in Locale analysis References :...

View Article


Generate a dropdown list of timezones in php

Applications often allow users to select their timezones for reporting the proper time properly. Here is a quick function that can be used to generate a dropdown list of timezones that is easy to read...

View Article


How to create tar archives in php

Tar is a common archive format used on linux. Alone it is just an archiving format, that is it can only pack multiple files together but not compress them. When combined with gzip or bzip2 files are...

View Article

How to extract tar.gz archives in php

In a previous article we learned how to . Now lets do the reverse, that is extract tar.gz archives and get the files out. The code to extract a tar.gz archive is very simple and uses PharData class....

View Article

How to compress images in php using gd

Php applications might need to do some image processing if they are allowing users to upload pictures of somekind. This can include cropping, watermarking, compressing etc. To compress an image the...

View Article


Download a file using curl in php

Here is a quick curl snippet for php, that can download a remote file and save it. <?php set_time_limit(0); //File to save the contents to $fp = fopen ('files2.tar', 'w+'); $url =...

View Article

Validate domain name using filter_var function in php

The filter_var function of php is capable of validating many things like emails, urls, ip addresses etc. It does not have a direct option to validate a domain name however. So I coded up this little...

View Article

Php – Fetch gzipped content over http with file_get_contents

The file_get_contents function is often used to quickly fetch a http url or resource. Usage is very simple and appears like this $content = file_get_contents('http://www.google.com/'); However the...

View Article


Php – Fix “Input is not proper UTF-8, indicate encoding” error when loading xml

When loading xml files in php through simplexml_load_string or domDocument class, sometimes an error like this might popup Warning: DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding...

View Article


Php – parse text and convert urls into hyperlinks

The following function will parse a given text and convert all the urls into links. It does this using regular expressions. It converts email addresses to mailto links as well. Code function...

View Article

PHP Format numbers to Indian Numerical System

The numerical system used in India differs from the Western system in terms of the placement of the thousands separator. Example : Number -> 1000000 Indian System -> 10,00,000 (Ten Lakh) Western...

View Article

PHP redirect – go back to previous page

To go back to the previous page the superglobal variable $_SERVER can be used. $_SERVER['HTTP_REFERER'] has the link to the previous page. So to redirect simply : // Method to go to previous page...

View Article

PHP create nested directories for a given path

If a file is to be saved in at path /var/www/a/b/c/d/abc.zip where the directory c and d dont exist then the directories have to created. Here is a function that uses recursion to check for directories...

View Article


PHP best way to check if file is an image

The getimagesize function of php provides lot of information about an image file , including its type. The type can be used to check if the file is a valid image file or not. To check if a file is an...

View Article

Execute shell commands in PHP

Like any other language php applications often need to execute system commands like they are run from the terminal/console/commandline. Php has multiple functions to do this task. Lets take a look at...

View Article


Php function to add st, nd, rd, th to the end of numbers

Often numbers are to be written with a proper suffix, like 1st, 2nd, 3rd, 15th, 21st and so on. So here is a quick function to do the same for a number. Technique 1 /* This function will add st, nd,...

View Article

Php : Get links on a page with DomDocument

Scraper scripts often need to extract all links on a given page. This can be done in a number of ways like regex, domdocument etc. Here is simple code snippet to do this using domdocument. /* Function...

View Article


Php – Extract name and value of all input tags with DomDocument

The DomDocument class in php can be used to parse html/xml content and build objects that can be queried using standard functions to extract values. The following functions are quite useful:...

View Article

Fill text templates using arrays in php

Text templates are often used to generate content like notification emails, invoices filled with the details of a customer or user. The best example would be a bulk email program that sends out emails...

View Article
Browsing latest articles
Browse All 20 View Live