One of the most common things any programmer will have to do is work with strings. Whether it be validating, checking value or sanitizing. Something I find helpful is to wrap functionality in classes so I can easily reuse them later. Here are a few string functions that I’ve found helpful. Don’t hesitate to comment with string functions you’ve found helpful!
class string { /** * Checks to see if string is only alphabetic * * @param string $value * @param boolean $ignore_spaces * @return boolean */ static function isAlpha($value, $ignore_spaces = false) { if (!isset($value)) { return false; } if ($ignore_spaces) { $value = str_replace(" ", "", $value); } return ctype_alpha($value); } /** * Checks to see if string only contains letters and numbers * * @param string $value * @param boolean $ignore_spaces * @return boolean */ static function isAlphaNum($value, $ignore_spaces = false) { if (!isset($value)) { return false; } if ($ignore_spaces) { $value = str_replace(" ", "", $value); } return ctype_alnum($value); } /** * Checks to see if a string is numeric * * @param string $value * @param boolean $ignore_spaces * @return boolean */ static function isNumeric($value, $ignore_spaces = false) { if (!isset($value)) { return false; } if ($ignore_spaces) { $value = str_replace(" ", "", $value); } return ctype_digit($value); } /** * Checks to see if string is a valid phone number * * @param string $value * @return boolean */ static function isPhone($value) { if (!isset($value)) { return false; } if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$", $value) || ereg("^\([0-9]{3}\)-[0-9]{3}-[0-9]{4}$", $value) || ereg("^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$", $value) || ereg("^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$", $value) || ereg("^[0-9]{3}[0-9]{3}[0-9]{4}$", $value) || ereg("^[0-9]{3} [0-9]{3} [0-9]{4}$", $value) ) { return true; } else { return false; } } /** * Checks to see if a string is an email * * @param string $value * @param boolean $check_domain * @return boolean */ public static function isEmail($value, $check_domain = false) { if (!isset($value)) { return false; } if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $value)) { if ($check_domain == true) { list($userName, $mailDomain) = split("@", $value); if (checkdnsrr($mailDomain, "MX")) { return true; } else { return false; } } return true; } else { return false; } } /** * Checks to see if a string is a valid zip code * * @param string $value * @param boolean $extended * @return boolean */ static function isZip($value, $extended = false) { if (!isset($value)) { return false; } if (!$extended) { if(ereg("^[0-9]{5}$", $value)) { return true; } else { return false; } } else { if(ereg("^[0-9]{5}$", $value) || ereg("^[0-9]{5}-[0-9]{4}$", $value)) { return true; } else { return false; } } } /** * Validates credit card number * * @param string $value * @return boolean */ static function isCreditCard($value) { if (!isset($value)) { return false; } if (ereg("(^(4|5)\d{3}-?\d{4}-?\d{4}-?\d{4}|(4|5)\d{15})|(^(6011)-?\d{4}-?\d{4}-?\d{4}|(6011)-?\d{12})|(^((3\d{3}))-\d{6}-\d{5}|^((3\d{14})))", $value)) { return true; } else { return false; } } /** * Strips html out of a string * * @param string $value */ static function stripHTML(&$value) { if (!isset($value)) { exit; } $breaks[] = "<br>"; $breaks[] = "<br />"; $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript '@<style[^>]*?>.*?</style>@siU' // Strip style tags properly ); $value = preg_replace($search, '', $value); $value = str_ireplace($breaks, "\r\n", $value); $value = trim($value); $value = strip_tags($value); $value = html_entity_decode($value, ENT_QUOTES); $value = addslashes($value); } static function cleanHTML(&$value) { $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript '@<style[^>]*?>.*?</style>@siU' // Strip style tags properly ); $value = preg_replace($search, '', $value); $value = htmlentities($value); } }
