Remove Space or Special characters

so if you have number of string like

$numberString = +1-800-123-(456);

after calling through function clean($numberString);

Result: 1800123456

function clean($string) {
	$string = str_replace(' ', '', $string); // Replaces all spaces.
	$string = preg_replace('/([^A-Za-z0-9\-])/', '', $string); // Removes special chars.
	return preg_replace('/-+/', '', $string); // Replaces multiple hyphens with single one.
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s