We are updating a system where notes were added to fields containing phone numbers. Using PHP we are trying to clean up the fields and split them into two. One for the phone number and the other for the note. The number is always first and the note after.
We are not overly concerned with the exact format of the resulting phone number. Users can be forced to clean them up when they update their profile. The numbers are US format.
A couple of examples. I imagine there could be other variations:
"(123) 456-7890 Betty's cell"
becomes
"(123) 456-7890" and "Betty's cell"
"123-456-7890 Betty's cell
becomes
"123-456-7890" and "Betty's cell"
"456-7890 Betty's cell
becomes
"456-7890" and "Betty's cell"
"456-7890 ext. 123 Betty's cell
becomes
"456-7890 ext. 123" and "Betty's cell"
Valid phone number characters would be "+()-0123456789 "
and to complicate things further we would need to allow for "ext."
I can sanitize the existing data so all ext. variations are the same. We would be happy to find the position of the first "invalid" character in the string and split it there.
Been searching but can't seem to find anything that fits this situation. Appreciate any suggestions.
Many Thanks!
You can use Regex such as like below;
^([\+\(\)\-0-9 ]*)([A-Za-z' ]*)$
Group1 result always number and Group2 result will be the name and surname You can check on https://regex101.com/r/PhEQNH/1/
$re = '/^([\+\(\)\-0-9 ]*)([A-Za-z\' ]*)$/';
$str = '123-456-7890 Betty\'s cell
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);