Skip to main content

Posts

Showing posts with the label CodeSnippets

Better Broken Image Handling

Missing images will either just display nothing, or display a [ ? ] style box when their source cannot be found. Instead, you may want to replace that with a "missing image" graphic that you are sure exists so there is better visual feedback that something is wrong. Or, you might want to hide it entirely. This is possible because images that a browser can't find fire off an "error" JavaScript event we can watch for. // Replace source $('img').on("error", function() { $(this).attr('src', '/images/missing.png'); }); // Or, hide them $("img").on("error", function() { $(this).hide(); }); Additionally, you may wish to trigger some kind of Ajax action to send an email to a site admin when this occurs.

Convert String to Sentence Case in PHP

In sentence case, the first character of an English sentence is capitalised and subsequent characters are lowercased, except for proper nouns and other words which are generally capitalised by a more specific rule. Converting string to sentence case is used in some cases in the web project. For example, you’ll display the string and text in the web page which had taken from the user, and you are not sure whether the text content case is properly formatted or not. On that case, you need to convert text to sentence case before displaying in the web page. Using PHP, you can easily convert any string to sentence case. In this article, we are going to show you a simple PHP script for convert string to sentence case. For better usability, all PHP code will be grouped together in a function and you only need to use this function for convert text to sentence case. sentenceCase() function accept one parameter ( $string ) and convert passed parametter value to sentence case. This function retur...

Some PHP Hacks Every Programmer Should Know

A lot of things are there which you should follow when working with PHP. In this article, we’ll discuss some important PHP hacks among them which every programmer should know about. These PHP tips will help you to write a more structured PHP script in less time. You should always use the following PHP hacks while coding in PHP. 1. Ternary Operator Ternary operator consistent with three expressions separated by a question mark (?) and a colon (:). It makes if/else logic shorter, quicker, and easier. Use ternary operator in you script to save line and script length as long as they are simple. $name = !empty($_GET['name'])? $_GET['name'] : 'CodexWorld'; 2. PHP Exception Handler When an error occurred PHP script displays a fatal error. To avoid the error you should need to write the proper code to handle the exception in PHP script. PHP exception handler is a smart way to handle the exceptional condition. //trigger exception in try block and catch exception try { ...

PHP Time Ago Script

I have received a tutorial requests from my reader, he asked me how to create facebook style PHP Time Ago script. So here is solution and this is very helpful for blogs and social media scripts. In this script I have created one function php_time_ago. This function required one parameter time in Unix timestamp and will return time ago statement. function php_time_ago($provided_time){ $time_difference = time() - $provided_time ; $seconds = $time_difference ; $minutes = round($time_difference / 60 ); $hours = round($time_difference / 3600 ); $days = round($time_difference / 86400 ); $weeks = round($time_difference / 604800 ); $months = round($time_difference / 2419200 ); $years = round($time_difference / 29030400 ); if($seconds <= 60){ echo "$seconds seconds ago"; }else if($minutes <=60){ if($minutes==1){ echo "one minute ago"; }else{ echo "$minutes minutes ago"; } }else if($hours <=24){ ...