First of all i would like to thanks to this blog ( http://www.catswhocode.com/blog/10-life-saving-php-snippets ) for giving this very nice php snippets info to keep. (w/c took me to other php site codes to ponder) I am not a hardcoder of php so i would like to keep this one for my future references. Now, time to go through my notes…
- AUTOMATIC PASSWORD CREATION - Generating passwords is a common task on membership sites. The following function will generate a password according to two parameters: length and strength.
- http://www.phpsnippets.info/generate-a-password-in-php
function generatePassword($length=9, $strength=0) {
$vowels = ‘aeuy’;
$consonants = ‘bdghjmnpqrstvz’;
if ($strength >= 1) {
$consonants .= ‘BDGHJLMNPQRSTVWXZ’;
}
if ($strength >= 2) {
$vowels .= “AEUY”;
}
if ($strength >= 4) {
$consonants .= ’23456789′;
}
if ($strength >= 8 ) {
$vowels .= ‘@#$%’;
}$password = ”;
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
- CALCULATE AGE USING DATE OF BIRTH – Simple but incredibely useful snippet for social networks: Give the date of birth of a person to this function, it will return that person age.
- http://www.phpsnippets.info/calculate-age-of-a-person-using-date-of-birth
function age($date){
$year_diff = ”;
$time = strtotime($date);
if(FALSE === $time){
return ”;
}$date = date(‘Y-m-d’, $time);
list($year,$month,$day) = explode(“-”,$date);
$year_diff = date(“Y”) – $year;
$month_diff = date(“m”) – $month;
$day_diff = date(“d”) – $day;
if ($day_diff < 0 || $month_diff < 0) $year_diff–;return $year_diff;
}
- COMPRESS MULTIPLE CSS FILES – In order to save your precious bandwidth, you should compress your css files. Doing so is not hard at all using this snippet.
- http://www.phpsnippets.info/compress-css-files-using-php
header(‘Content-type: text/css’);
ob_start(“compress”);
function compress($buffer) {
/* remove comments */
$buffer = preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”, $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array(“\r\n”, “\r”, “\n”, “\t”, ‘ ‘, ‘ ‘, ‘ ‘), ”, $buffer);
return $buffer;
}/* your css files */
include(‘master.css’);
include(‘typography.css’);
include(‘grid.css’);
include(‘print.css’);
include(‘handheld.css’);ob_end_flush();
- MAINTENANCE MODE WITH PHP – When updating or modifying your site, it can be extremely useful to have a function which temporarily redirect your readers to a maintenance page.
- http://www.phpsnippets.info/easy-maintenance-mode-with-php
function maintenance($mode = FALSE){
if($mode){
if(basename($_SERVER['SCRIPT_FILENAME']) != ‘maintenance.php’){
header(“Location: http://example.com/maintenance.php”);
exit;
}
}else{
if(basename($_SERVER['SCRIPT_FILENAME']) == ‘maintenance.php’){
header(“Location: http://example.com/”);
exit;
}
}
}
- HIGHLIGHT SPECIFIC WORDS IN A PHRASE – Sometimes it can be really usefull for a visitor to have the terms he serached for highlighted. The following snippet is using a simple regular expression to find words in a phrase and highlight them.
- http://www.phpsnippets.info/highlights-words-in-a-phrase
function highlight($sString, $aWords) {
if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
return false;
}$sWords = implode (‘|’, $aWords);
return preg_replace (‘@\b(‘.$sWords.’)\b@si’, ‘<strong style=”background-color:yellow”>$1</strong>’, $sString);
}
Well so far these are the snippets that i needed most and I wanna keep on my blog. And If you guys need more of the codes then you can just click on the links above for more of it.



