Tags

Related Posts

Share This

PHP, Common String Functions

There are always those functions that you need to manipulate strings for whatever reason. Maybe it”s for validation purposes or sanitizing purposes or some kind of string conversion or searching. Here is a class that encapsulates alot of common string functions that I use alot. All functions that start with “is” are boolean functions:

class string
{
/**
* Checks to see if string is only alphabetic
*
* @static
* @param string $value
* @param boolean $ignore_spaces
* @return boolean
* @category string
*/
static function isAlpha($value, $ignore_spaces = false)
{
if (!isset($value{0}))
{
return false;
}
if ($ignore_spaces)
{
$value = str_replace(" ", "", $value);
}
return ctype_alpha($value);
}
/**
* Checks to see if string only contains letters and numbers
*
* @static
* @param string $value
* @param boolean $ignore_spaces
* @return boolean
* @category string
*/
static function isAlphaNum($value, $ignore_spaces = false)
{
if (!isset($value{0}))
{
return false;
}
if ($ignore_spaces)
{
$value = str_replace(" ", "", $value);
}
return ctype_alnum($value);
}
/**
* Checks to see if a string is a hex
* @param string $value
* @return boolean
*/
static function isHex($value)
{
if (!isset($value{0}))
{
return false;
}
return ctype_xdigit($value);
}
/**
* Checks to see if a string is numeric
*
* @static
* @param string $value
* @param boolean $ignore_spaces
* @return boolean
* @category string
*/
static function isNumeric($value, $ignore_spaces = false)
{
if (!isset($value{0}))
{
return false;
}
if ($ignore_spaces)
{
$value = str_replace(" ", "", $value);
}
return ctype_digit($value);
}
static function isDate($value)
{
if (!isset($value{0}))
{
return false;
}
if (strtotime($value) !== false)
{
return true;
}
else
{
return false;
}
}
/**
* Checks to see if string is a valid phone number
*
* @static
* @param string $value
* @return boolean
* @category string
*/
static function isPhone($value)
{
if (!isset($value{0}))
{
return false;
}
if(ereg("^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$", $value))
{
return true;
}
else
{
return false;
}
}
/**
* Checks to see if a string is an email
*
* @static
* @param string $value
* @param boolean $check_domain
* @return boolean
* @category string
*/
public static function isEmail($value, $check_domain = false)
{
if (!isset($value{0}))
{
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
*
* @static
* @param string $value
* @param boolean $extended
* @return boolean
* @category string
*/
static function isZip($value, $extended = false)
{
if (!isset($value{0}))
{
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
*
* @static
* @param string $value
* @return boolean
* @category string
*/
static function isCreditCard($value)
{
if (!isset($value{0}))
{
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
*
* @static
* @param string $value
* @category string
*/
static function stripHTML(&$value)
{
if (!isset($value{0}))
{
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);
}
/**
* Cleans out script tags out of HTML
*
* @static
* @param string $value
* @category string
*/
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);
}
/**
* Encodes all non alpha numeric characters in a URL with the '%' sign
*
* @static
* @param string $url
* @return string
* @example string.php how to use;
* @category string
*/
static function UrlEncode($url)
{
if (strpos($url, '?') === false)
{
return $url;
}
else
{
$startpos  = strpos($url, "?");
$tmpurl    = substr($url, 0 , $startpos+1);
$qryStr    = substr($url, $startpos+1);
$qryvalues = explode("&", $qryStr);
foreach($qryvalues as $value)
{
$buffer    = explode("=", $value);
$buffer[1] = urlencode($buffer[1]);
$new_query_values[] = implode("=", $buffer);
}
$finalqrystr =implode("&", $new_query_values);
$finalURL    =$tmpurl . $finalqrystr;
return $finalURL;
}
}
/**
*
* @param $length
* @return string
*/
static function generateID($length)
{
$random_id_length = $length;
$rnd_id = crypt(uniqid(rand(),1));
$rnd_id = strip_tags(stripslashes($rnd_id));
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));
$rnd_id = substr($rnd_id,0,$random_id_length);
return $rnd_id;
}
}

This class has been documented using the phpdoc specification so that if you are using an IDE such as Eclipse PDT it will give you more direction. This class ecapsulates all of these functions and mimics namespaces for PHP. PHP 5.3 and above has namespaces as a part of the core of PHP, but for right now, static methods are the way to go for compatability sake.