Common PHP Functions
Categories
Total functions: [48]
Descriptions coming soon
function countWords($words) { $result = array(); $unique = array_unique($words); $unique = array_values($unique); // loop through the unique words to count them in the original words for($x = 0; $x < count($unique); $x++) { $result[0][$x] = $unique[$x]; $result[1][$x] = 0; foreach($words as $w) { if($unique[$x] == $w) { $result[1][$x] += 1; } } } return $result; }
function getWords($string) { $words = explode(' ', trim($string)); foreach($words as $key=>$word) { $last_char = $word[strlen($word)-1]; $punctuations = '.;:!?,'; // remove any punctuations or chracters from the end of the word $words[$key] = rtrim($word, $punctuations); } return $words; }
function getSentences($string) { preg_match_all('~.*?[?.!]~s', $string, $sentences); return array_map('trim', $sentences[0]); }
function capitalizeWords($words = array(), $str) { if(!empty($words) && $str != '') { foreach($words as $word) { $str = str_replace($word, ucwords($word), $str); } } return $str; }
function ipAddress() { return $_SERVER['REMOTE_ADDR']; }
function getColorShade($color) { $colors = array(); for($x = 0; $x < 255; $x++) { $c = ''; switch($color) { case 'red': $c = dechex($x).'0000'; break; case 'green': $c = '00'.dechex($x).'00'; break; case 'blue': $c = '0000'.dechex($x); break; default: $c = '000000'; } if(strlen($c) < 6) { $c .= '0'; } $colors[$x] = $c; } // remove duplicates and reset the indexes $colors = array_unique($colors); $colors = array_values($colors); sort($colors); return $colors; }
function camelizeString($string) { $string = trim($string); $result = ''; $space_found = (strpos($string, ' ') !== FALSE); // 1 or more spaces found in the string $underscore_found = (strpos($string, '_') !== FALSE); // 1 or more underscores found in the string if($space_found || $underscore_found) { if($space_found && $underscore_found) { $split_str = split('[ _]', $string); } else if($space_found) { $split_str = split(' ', $string); } else if($underscore_found) { $split_str = split('_', $string); } $result = strtolower($split_str[0]); for($x = 1; $x < count($split_str); $x++) { $result .= ucwords($split_str[$x]); } } else { $result = strtolower($string); } return $result; }
function humanizeString($string) { return str_replace('_', ' ', $string); }
function underscoreString($string) { return str_replace(' ', '_', $string); }
function jsRedirect($url) { ?> <script type="text/javascript"> window.location = "<?php echo $url; ?>"; </script> <?php }
function filterURL($url) { if(strpos($url, 'http://') !== FALSE) { return $url; } else { if($url == '') { return ''; } else { return 'http://'.$url; } } }
function isWeekday($date) { $d = date('w', strtotime($date)); if($d != 6 && $d != 0) return TRUE; else return FALSE; }
function isWeekend($date) { $d = date('w', strtotime($date)); if($d == 6 || $d == 0) return TRUE; else return FALSE; }
function removeCharacters($char, $subject) { $subject = str_replace($char, '', $subject); return $subject; }
function filterWords($words = array(), $filter_str, $subject) { foreach($words as $word) { $subject = str_replace($word, $filter_str, $subject); } return $subject; }
function truncateWords($subject, $word_ct) { $str_word_count = str_word_count($subject); if($str_word_count < $word_ct) { return $subject; } else { $words = split(' ', $subject); $result_str = ''; for($x = 0; $x < $word_ct; $x++) { $result_str .= $words[$x].' '; } return trim($result_str); } }
function checkDuplicates($subject) { $words = split(' ', $subject); $temp = array(); $duplicates = array(); if(!empty($words)) { foreach($words as $word) { if(in_array($word, $temp)) { if(!in_array($word, $duplicates)) { $duplicates[] = $word; } } else { $temp[] = $word; } } } return $duplicates; }
function repeat($str, $number) { for($x = 0; $x < $number; $x++) { echo $str; } }
function averageWordCount($strings = array()) { $word_ct = 0; if(!empty($strings)) { for($x = 0; $x < count($strings); $x++) { $word_ct += str_word_count($strings[$x]); } return $word_ct/count($strings); } else { return 0; } }
function averageCharCount($str) { $char_ct = 0; if($str != '') { $words = split(' ', trim($str)); for($x = 0; $x < count($words); $x++) { $char_ct += strlen($words[$x]); } return $char_ct/count($words); } else { return 0; } }
function areaOfTriangle($base, $height) { return ($base * $height) / 2; }
function circumferenceOfCircle($diameter) { return pi() * $diameter; }
function areaOfCircle($radius) { return pi() * ($radius * $radius); }
function redirect($url) { header('Location: '.$url); }
function validateEmail($email) { if(preg_match('/^[^@]+@[^@.]+\.[^@]*\w\w$/', $email)) { return TRUE; } else { return FALSE; } }
function generateRandomCode($length) { $code = ''; $possible = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; for($x = 0; $x < $length; $x++) { $random = mt_rand(0, strlen($possible)-1); $code .= $possible[$random]; } return $code; }
function decrypt($key, $encrypted_str) { $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted_str), MCRYPT_MODE_CBC, md5(md5($key))), "\0"); return $decrypted; }
function encrypt($key, $str) { $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $str, MCRYPT_MODE_CBC, md5(md5($key)))); return $encrypted; }
function getRandomChar($str, $exclude_spaces = TRUE) { $random_num = mt_rand(0, strlen($str)-1); $random_char = $str[$random_num]; if($exclude_spaces) { if($random_char == ' ') { $random_char = getRandomChar($str, $exclude_spaces); } } return $random_char; }
function getTimeDiff($start, $end) { $uts['start'] = strtotime($start); $uts['end'] = strtotime($end); if($uts['start'] !== -1 && $uts['end'] !== -1) { if($uts['end'] >= $uts['start']) { $diff = $uts['end'] - $uts['start']; if($days=intval((floor($diff/86400)))) $diff = $diff % 86400; if($hours=intval((floor($diff/3600)))) $diff = $diff % 3600; if($minutes=intval((floor($diff/60)))) $diff = $diff % 60; $diff = intval($diff); return(array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff)); } else { trigger_error("Ending date/time is earlier than the start date/time", E_USER_WARNING); } } else { trigger_error("Invalid date/time data detected", E_USER_WARNING); } return(FALSE); }
function calcFactorial($number) { $result = 1; for($x = 1; $x <= $number; $x++) { $result *= $x; } return $result; }
function calcFOIL($a, $b, $c, $d) { $first = $a*$c; $outer = $a*$d; // x $inner = $b*$c; // x $last = $b*$d; // x^2 return $last.'x<sup>2</sup> '.($outer+$inner).'x '.$first; }
function getEfficiency($horsepower, $watts) { return round((746 * $horsepower) / $watts, 4); }
function getVoltage($current, $resistance) { return round($current * $resistance, 4); }
function getResistance($voltage, $current) { return round($voltage / $current, 4); }
function getCurrent($voltage, $resistance) { return round($voltage / $resistance, 4); }
function getWatts($voltage, $current) { return round($voltage * $current, 4); }
function convertTemperature($temp) { $result = 0; // get the temperature unit $unit = strtolower($temp[(strlen($temp)-1)]); $number = substr($temp, 0, (strlen($temp)-1)); switch($unit) { case 'c': $result = ($number * 1.8) + 32; break; case 'f': $result = ($number - 32) * 1.8; break; default: return FALSE; break; } return $result; }
function flipCoin() { $possible = array('heads', 'tails'); $key = array_rand($possible); return $possible[$key]; }
function randomColor() { $color = ''; for($x = 0; $x < 3; $x++) { $rand_hex = dechex(rand(0, 255)); // check if the value is only one digit // if so, prepend zero to it if(strlen($rand_hex) < 2) { $rand_hex = '0'.$rand_hex; } $color .= $rand_hex; } return '#'.$color; }
function averageDifference($numbers = array()) { if(!empty($numbers)) { sort($numbers); $differences = array(); // loop through the numbers and find the differences for($x = (count($numbers)-1); $x >= 0; $x--) { if(is_numeric($numbers[$x])) { // check if there is a next number to subtract if($x != 0) { $differences[] = $numbers[$x]-$numbers[($x-1)]; } } } // now we simply get the average of the differences if the previous loop was successful if(!empty($differences)) { $sum = array_sum($differences); return round($sum / count($differences), 4); } else { return FALSE; } } else { return FALSE; } }
function validateNumber($string, $min = 1, $max = 99) { if(!is_numeric($string)) { return FALSE; } else { if($string < $min || $string > $max) { return FALSE; } } return TRUE; }
function getRGB($hexidecimal) { $hexidecimal = str_replace('#', '', $hexidecimal); $chunks = str_split($hexidecimal, 2); // convert to decimal foreach($chunks as $key=>$val) { $chunks[$key] = hexdec($val); } return array('red'=>$chunks[0], 'blue'=>$chunks[1], 'green'=>$chunks[2]); }
function sumSeries($min = 1, $max = 100) { $result = 0; for($x = $min; $x <= $max; $x++) { $result += $x; } return $result; }
function scrambleString($string) { $str1 = ''; $str2 = ''; $i = 0; $x = 0; $string = explode(' ', $string); $result = ''; for ($x = 0; $x < strlen($string); $x++) { $tmp = $string[$x]; srand((double) microtime() * 10000000); for ($i = 0; $i < strlen($tmp); $i++) { $str1 = rand(0, strlen($tmp) - 1); $str2 = $tmp[$i]; $tmp[$i] = $tmp[$str1]; $tmp[$str1] = $str2; } $result .= ' '.$tmp; } return $result; }

