7 Handy PHP Code Snippets to Save Time

Snippets are often helpful in adding small features to projects or apps in no time. One simply needs to copy and paste the snippet in their projects or apps, and voila!

In this article, I will be featuring 7 awesome and really handy code snippets in PHP, which might prove to be a super time saver for you.

1) Automatic Word Sensor

This code snippet works like a charm to automatically censor words which you define in an array by replacing them with other words which you can choose to set.

[php] [/php]

2) Crop Images

Crop Images

This code snippet lets you easily crop images from a specified X and Y coordinates and a specified size. This snippet is obtained from css-tricks.com.

[php] [/php]

3) Time to Words

Time to Words

This code snippet will convert any given time in numeric format to the corresponding time in formatted words and numbers, like the above example. This snippet is obtained from viralpatel.net.

[php] function secsToStr($secs) {
if($secs>=86400){
$days=floor($secs/86400);
$secs=$secs%86400;
$r=$days.’ day’;
if($days<>1){$r.=’s’;}
if($secs>0){$r.=’, ‘;}
}
if($secs>=3600){
$hours=floor($secs/3600);
$secs=$secs%3600;
$r.=$hours.’ hour’;
if($hours<>1){$r.=’s’;}
if($secs>0){$r.=’, ‘;}
}
if($secs>=60){
$minutes=floor($secs/60);
$secs=$secs%60;
$r.=$minutes.’ minute’;
if($minutes<>1){$r.=’s’;}
if($secs>0){$r.=’, ‘;}
}
$r.=$secs.’ second’;
if($secs<>1){$r.=’s’;}
return $r;
} [/php]

4) Date Format Validation

Date Validation

This code snippet checks whether an input date is given in a specific format or not, which in our case is YYYY/MM/DDThis snippet is obtained from viralpatel.net.

[php] function checkDateFormat($date) {
//match the format of the date
if (preg_match (“/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/”, $date, $parts)) {
//check whether the date is valid or not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
}
else
return false;
} [/php]

5) Check for Opened Email

Email Status

This code snippet lets you check whether your sent email has been opened or not by the recipient. You need to include the following line of HTML code within your email and then save the rest in an image.php file. This snippet is obtained from emoticode.net.

<img src=”http://www.yourhost.com/image.php”>

Copy the following within image.php,

[php] [/php]

6) Force File Download

Force Download

This code snippet forces your users to download a specified file instead of viewing directly within the browser. For specifying forced downloads you need to add links in the following manner,

<a href=”downloadFile.php?file=7383″>Download</a>

Here downloadFile.php will contain the following lines of code.

[php] 0){
$row = mysql_fetch_array($sql);
// Set some headers
header(“Pragma: public”);
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Content-Type: application/force-download”);
header(“Content-Type: application/octet-stream”);
header(“Content-Type: application/download”);
header(“Content-Disposition: attachment; filename=”.basename($row[‘FileName’]).”;”);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “.filesize($row[‘FileName’]));
@readfile($row[‘FileName’]);
exit(0);
}
else {
header(“Location: /”);
exit(0);
}
?> [/php]

7) Display your Latest Tweet

Latest Tweet

This code snippet will display the latest tweet from a specified twitter username. You can set your preferred username in the username variable. This snippet is obtained from perishablepress.com.

[php] corePHP

‘;
// $suffix = ‘

Follow us on Twitter

‘;
function parse_feed($feed) {
$stepOne = explode(‘‘, $feed);
$stepTwo = explode(‘
‘, $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace(“<“, “<", $tweet); $tweet = str_replace(">", ">“, $tweet);
return $tweet;
}
$latest_tweet = file_get_contents($feed);
// echo stripslashes($prefix) . parse_feed($latest_tweet) . stripslashes($suffix);
echo parse_feed($latest_tweet);
?> [/php]

Wrapping Up

This brings us to the end of this list of some handy code snippets. I hope that these snippets will prove to be helpful for you and save your precious development time. In meantime, why not tell us which snippets have you tried out?

STAY UP TO DATE

Sign up today to stay informed with industry news & trends