Connect with us

PHP string processing functions class

solution

PHP string processing functions class

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);
	}
}

 

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in solution

    About Me:

    Szabi Kisded

    Hey there, I'm Szabi. At 30 years old, I quit my IT job and started my own business and became a full time WordPress plugin developer, blogger and stay-at-home dad. Here I'm documenting my journey earning an online (semi)passive income. Read more

    Sign up for my newsletter and get the YouTube Caption Scraper WordPress plugin for free
    (worth 29$)!

    All My Plugins In A Bundle:

    My AutoBlogging Plugins:

    My Online Courses:

    A Theme I Recommend:

    Featured Posts:

    To Top