Jamie Belcher
Jamie Belcher

Reputation: 123

Removing (nearly) all non-numeric characters from a string

So I've created a number format generator which will take human telephone numbers, and apply the appropriate format following the HSCIC Rules and Regulations, one issue I'm having is number validation.

For instance, I get to a step, just after removing all whitespace, and I'd like to check the number once more, removing every character except: Numbers: 0 - 9 Letters: E, X, T - case insensitive Special: +, :

I've looked online but I can't find a way to only keep these few characters. All help is appreciated!

Upvotes: 1

Views: 255

Answers (1)

DaTebe
DaTebe

Reputation: 722

If I understand you correctly you can use this:

$re = "/[0-9XxTtEe+:]*/"; 
$str = "394160etg9834ztg";  // <-- User Input

preg_match_all($re, $str, $matches);

In $matches should be all characters that are allowed. Just combine the results that are matched in $matches

Upvotes: 2

Related Questions