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. The solution to fix this error is quite simple. Just convert [...]
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 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 http headers. Compressing the content saves bandwidth and speeds up the transfer process. So the trick [...]
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. Now use it as Last Updated On : 21st March 2013
Download a file using curl in php
Here is a quick curl snippet for php, that can download a remote file and save it. The CURLOPT_FILE option takes a file resources and writes the content of the url to that file resource/handle. Also set the script time limit to something large so that the script does not end when downloading large files. Last Updated On : 19th March 2013
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 Last Updated On : 14th March 2013
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 The above function will process a jpg/gif/png image and compress it and save to jpg format. The actual compression takes place in just oneline, that is the imagejpeg function. The quality value controls the amount [...]
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 The first step is to uncompress the tar.gz and convert into a .tar file. Next the .tar file is unpacked and file are copied inside a directory. Last Updated On : 12th March [...]
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 be used to create tar archives and tar.gz compressed files as well. It works [...]
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. Please upgrade your browser Last Updated On : 22nd November 2012
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 . Last Updated On : 22nd November 2012