Browsing articles tagged with

php code snippets

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' , ...

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, rd, th to the end of numbers */ function ordinal($i) { $l = substr($i,-1); $s = substr($i,-2,-1); return (($l==1&&$s==1)||($l==2&&$s==1)||($l==3&&$s==1)||$l>3||$l==0?'th':($l==3?'rd':($l==2?'nd':'st'))); } /* Example usage */ for($i = 0; $i < 100; $i++) { echo $i . ordinal($i). ...

Quick Tip: Getting the Next and Previous Items or Posts

It is a common requirement in lots of projects to fetch the next and previous items or posts and display a link to them on every item or post page. Getting the previous and next item can be quite tricky especially if they need to be user-specific, although it is very easy. I'll show you how - function nextItem($item_id) { ...

Set mysql timezone at runtime via PHP

To set the timezone of mysql at runtime via php use the following code : $c = mysqli_connect($host, $username, $password); mysqli_query($c , "SET `time_zone` = '".date('P')."'"); This can be executed when the connection is established and it will set the timezone of mysql to whatever timezone php is set to. Fast and effective. ...

PHP download remote file via url

The following function will download and save a remote file , using its http url : function download_remote($url , $save_path) { $f = fopen( $save_path , 'w+'); $handle = fopen($url , "rb"); while (!feof($handle)) { $contents = fread($handle, 8192); fwrite($f , $contents); } fclose($handle); fclose($f); } The method reads 8192 bytes at a time , and not the whole file , since if the whole file is read at once , then ...

PHP redirect – go back to previous page

To go back to the previous page the superglobal variable $_SERVER can be used. $_SERVER has the link to the previous page. So to redirect simply : #Method to go to previous page function goback() { header("Location: {$_SERVER}"); exit; } goback(); ...

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 System -> 1,000,000 (1 Million) While echoing numbers in PHP it is possible to format numbers with such separators using functions like number_format. To format a number in the Indian Numerical System ...

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 parse_links($str) { $str = str_replace('www.', 'http://www.', $str); $str = preg_replace('|http://(+)|', '<a href="http://$1">$1</a>', $str); $str = preg_replace('/((+)(.+)*@(+.)+{2,6})/', '<a href="mailto:$1">$1</a>', $str); ...



Connect with us