New API: Expand URL

Today, the Internet is flooded with shortened URL. All links shared on Twitter are shortened (start with There is nothing wrong with it. I just don’t feel comfortable clicking a shortened URL without knowing where it will bring me to. How paranoid I am! There are several reason I don’t trust shortened URL: They might contain tracking or referral code. SlickDeals heavily uses it (to make money, of course). Again, there is nothing wrong with it. They provide you a service and they deserve to get some money out of it. They might hide malicious URL, like XSS I hate them! There are several expand URL services out there, however they just support a very limited set of URL shorten service. What I want is something more. I want to be able to track the final URL after a bunch of redirections. So I wrote this API:<your_url_here_no_escape_needed> For example: Here is the API result. You will be amazed how many hops it take to the final destination:!GQ96-PAKTEzy%3C1DD9%3A%2F%2FGGG.3x8A8wI.w86%3ASK%2Fw52w4-LMMPMQR-LKOOKSTR%3C%3Ca%3C1DD9C%3A%2F%2FGGG.08805y.w86%2F%3C Suggestions welcome!

How to format bytes to kilobytes, megabytes, gigabytes in PHP

This is the best function I found on StackOverflow: function formatBytes($bytes, $precision = 2) { $units = array(‘B’, ‘KB’, ‘MB’, ‘GB’, ‘TB’); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) – 1); // Uncomment one of the following alternatives // If 1 kilobytes = 1024 bytes // $bytes /= pow(1024, $pow); // If 1 kilobytes = 1000 bytes // $bytes /= (1 < < (10 * $pow)); return round($bytes, $precision) . ' ' . $units[$pow]; }

PHP: eval() without output to screen

Sometimes, we need to eval() some lines of code and grab the result without output to screen. As you know, eval() has only one parameter, so we have to use output buffer for that purpose. The following code can achieve that. Pretty clean and understandable: ob_start(); eval(‘echo “Hello world!”;’); $str = ob_get_contents(); ob_end_clean(); $str will hold the output returned by eval function.

List contents of a directory by date in PHP

Sometimes, you need to list all contents of a directory, for example, to display all images in a specified directory. Unfortunately, PHP native functions to handle files and directories have a very limited capabilities. For example readdir, opendir and much more only takes 1 parameter. You might wonder how to list contents of a directory by date. Here is one solution for that problem: function listdir_by_date($path){ $dir = opendir($path); $list = array(); while($file = readdir($dir)){ if ($file != ‘.’ and $file != ‘..’){ //To make sure you will not overwrite an array key, add filename at the end of datetime $ctime = filectime($data_path . $file) . ‘,’ . $file; $list[$ctime] = $file; } } closedir($dir); krsort($list); return $list; } Pretty simple, huh?

Remove one or more values from array in PHP

The solution is quite simple: Using built-in function array_diff of PHP: $dest_array = array_diff($src_array, array(“value_to_remove_1”, “value_remove_2”)); Of course you can use this method to remove just one value from array: $dest_array = array_diff($src_array, array(“value_to_remove”)); One problem of this method is that, there are some holes in $dest_array after apply this method. There are some ways of solving that problem: Remove all key strings from the array and replace them with numbers, use array_values Preserve the key names (strings), or reindex the array if all keys are numerical, use array_merge