1

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!

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

I'm trying to make sense of this function, I tend to steer clear of preg_match, so how does the $re work? And should $matches be an array?
$re is the regular expression applied on the input string. It matches all charcters from 0-9 and your special characters (The matching is done in preg_match_all). $matches is an "Array of all matches in multi-dimensional array ordered according to flags". Try a var_dump($matches) to see what datastructure is behind it and then work with the result.
I var dumped the area and I see it's split at each incorrect value, so naturally I'm attempting to implode the area to create it into a single string. But I'm being hit with the error "Error converting array to string on line XXX"
Try implode('', $matches[0]). For me the results where in index 0.
Yeah that was my mistake, I didn't notice it created two arrays! It's all working thank you! I'll mark this as the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.