Following is a list of some of the most useful PHP functions that a PHP developer will need at any point in his career. If you see any room for improvement, or if you can add something than go ahead and comment and I will definately give it a look for possible inclusion.
Removes slashes contained in a string or in an array
/**
* Strip Slashes
*
* Removes slashes contained in a string or in an array
*
* @param mixed string or array
* @return mixed string or array
*/
function strip_slashes($str)
{
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = strip_slashes($val);
}
}
else
{
$str = stripslashes($str);
}
return $str;
}
Removes single and double quotes from a string
/**
* Strip Quotes
*
* Removes single and double quotes from a string
*
* @param string
* @return string
*/
function strip_quotes($str)
{
return str_replace(array('"', "'"), '', $str);
}
Converts single and double quotes to entities
/**
* Quotes to Entities
*
* Converts single and double quotes to entities
*
* @param string
* @return string
*/
function quotes_to_entities($str)
{
return str_replace(array("\'","\"","'",'"'), array("'",""","'","""), $str);
}
Converts double slashes in a string to a single slash, except those found in http:// .
/**
* Reduce Double Slashes
*
* Converts double slashes in a string to a single slash,
* except those found in http://
*
* http://www.mypaaji.com//index.php
*
* becomes:
*
* http://www.mypaaji.com/index.php
*
* @param string
* @return string
*/
function reduce_double_slashes($str)
{
return preg_replace("#([^:])//+#", "\\1/", $str);
}
Reduces multiple instances of a particular character. Example: Sunny, Yuvi,, Ashwini, Sneha becomes: Sunny, Yuvi, Ashwini, Sneha
/**
* Reduce Multiples
*
* Reduces multiple instances of a particular character. Example:
*
* Sunny, Yuvi,, Ashwini, Sneha
*
* becomes:
*
* Sunny, Yuvi, Ashwini, Sneha
*
* @param string
* @param string the character you wish to reduce
* @param bool TRUE/FALSE - whether to trim the character from the beginning/end
* @return string
*/
function reduce_multiples($str, $character = ',', $trim = FALSE)
{
$str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
if ($trim === TRUE)
{
$str = trim($str, $character);
}
return $str;
}
Useful for generating passwords or hashes.
/** * Create a Random String * * Useful for generating passwords or hashes. * * @param string type of random string. Options: alunum, numeric, nozero, unique * @param integer number of characters * @return string */ function random_string($type = 'alnum', $len ={ switch($type) { case 'alnum' : case 'numeric' : case 'nozero' : switch ($type) { case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 'numeric' : $pool = '0123456789'; break; case 'nozero' : $pool = '123456789'; break; } $str = ''; for ($i=0; $i < $len; $i++) { $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1); } return $str; break; case 'unique' : return md5(uniqid(mt_rand())); break; } }
Supply a string and an array of disallowed words and any matched words will be converted to #### or to the replacement word you’ve submitted.
/**
* Word Censoring Function
*
* Supply a string and an array of disallowed words and any
* matched words will be converted to #### or to the replacement
* word you've submitted.
*
* @param string the text string
* @param string the array of censoered words
* @param string the optional replacement value
* @return string
*/
function word_censor($str, $censored, $replacement = '')
{
if ( ! is_array($censored))
{
return $str;
}
$str = ' '.$str.' ';
foreach ($censored as $badword)
{
if ($replacement != '')
{
$str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str);
}
else
{
$str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);
}
}
return trim($str);
}
Limits a string to X number of words.
/**
* Word Limiter
*
* Limits a string to X number of words.
*
* @param string
* @param integer
* @param string the end character. Usually an ellipsis
* @return string
*/
function word_limiter($str, $limit = 100, $end_char = '…')
{
if (trim($str) == '')
{
return $str;
}
preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
if (strlen($str) == strlen($matches[0]))
{
$end_char = '';
}
return rtrim($matches[0]).$end_char;
}
Limits the string based on the character count. Preserves complete words so the character count may not be exactly as specified.
/**
* Character Limiter
*
* Limits the string based on the character count. Preserves complete words
* so the character count may not be exactly as specified.
*
* @param string
* @param integer
* @param string the end character. Usually an ellipsis
* @return string
*/
function character_limiter($str, $n = 500, $end_char = '…')
{
if (strlen($str) < $n)
{
return $str;
}
$str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
if (strlen($str) <= $n)
{
return $str;
}
$out = "";
foreach (explode(' ', trim($str)) as $val)
{
$out .= $val.' ';
if (strlen($out) >= $n)
{
return trim($out).$end_char;
}
}
}
That’s it guys I will keep updating this post with more handy functions, Please contribute if you have any.
Share Some Love