Category : "Php Snippets"

Php code snippets and examples

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 ! 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 that do not fit in the utf-8 character set. ...

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 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 are asked to do so in the ...

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 snippet that the filter_var function with a little tweak so that it can validate domain names as well. function filter_var_domain($domain) { if(stripos($domain, 'http://') === 0) { $domain = substr($domain, ...

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 = "http://localhost/files.tar"; //Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ","%20",$url)); curl_setopt($ch, CURLOPT_TIMEOUT, 50); //give curl the file pointer so that it can write to it curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($ch);//get ...

Get the path of tmpfile in php

tmpfile is a very handy function to create temporary files. It returns the file handle/resource. If the path of the file is needed, for use with file_put_contents for example then here is a quick trick to do that $path = array_search('uri', @array_flip(stream_get_meta_data($GLOBALS=tmpfile()))); file_put_contents($path, 'hello'); echo file_get_contents($path); ...

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 quality needs to be adjusted. Here are few examples function compress_image($src, $dest , $quality) { $info = getimagesize($src); if ($info == 'image/jpeg') { $image = imagecreatefromjpeg($src); } elseif ($info == 'image/gif') { $image = imagecreatefromgif($src); } elseif ...

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. Here is an example // decompress from gz $p = new PharData('files.tar.gz'); $p->decompress(); // creates files.tar // unarchive from the tar $phar = new PharData('files.tar'); $phar->extractTo('new_dir'); The first step ...

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 compressed as well. Then it becomes a .tar.gz which is actually a gzip compressed tar file. Php has a class called PharData that can ...

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 and understand. <?php function get_timezones() { $o = array(); $t_zones = timezone_identifiers_list(); foreach($t_zones as $a) { $t = ''; try { //this throws exception for 'US/Pacific-New' $zone = new DateTimeZone($a); $seconds = $zone->getOffset( new DateTime("now" , $zone) ); $hours ...

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 : 1. http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 2. http://blog.xoundboy.com/?p=235 */ $language_codes = array( 'en' => 'English' , 'aa' => 'Afar' , 'ab' => 'Abkhazian' , 'af' => 'Afrikaans' , 'am' => 'Amharic' , 'ar' => 'Arabic' , 'as' => 'Assamese' , ...

Pages:123»