Category Archives: Php Snippets

Php code snippets and examples

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

By | March 26, 2013

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 ! OR Warning: simplexml_load_string(): Entity: line 93: parser error : Input is not proper UTF-8, indicate encoding ! The error occurs when the xml has some invalid characters… Read More »

Php – Fetch gzipped content over http with file_get_contents

By | March 24, 2013

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 file_get_contents does not get the contents compressed. It requests the server to send everything in plain text format. Most websites are capable of serving compressed content, if they… Read More »

How to download a file using Curl in PHP – Code Snippet

By | April 23, 2019

Download a File using Curl 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 = "http://localhost/files.tar"; // Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ","%20",$url));… Read More »

How to compress images in php using gd

By | March 12, 2013

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 quality needs to be adjusted. Here are few examples function compress_image($src, $dest , $quality) { $info = getimagesize($src); if ($info[‘mime’] == ‘image/jpeg’) { $image… Read More »

Php array of iso 639-1 language codes and names

By | November 22, 2012

Here is a php array containing the language codes and language names as specified by the iso 639-1 standard. Useful when . &lt;?php /** ISO 639-1 Language Codes Useful in Locale analysis References : 1. http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 2. http://blog.xoundboy.com/?p=235 */ $language_codes = array( ‘en’ =&gt; ‘English’ , ‘aa’ =&gt; ‘Afar’ , ‘ab’ =&gt; ‘Abkhazian’ , ‘af’… Read More »

Fill text templates using arrays in php

By | June 9, 2013

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 to multiple users filling the details of the particular user in every individual mail. Text templates have place holders for fields… Read More »