I'm trying to figure out how to convert a phone number in the format
+18761234567
into
876-123-4567
using a replace Regex.
I think this should work
preg_replace('/^\+1(\d{3})(\d{3})(\d{4})$/i', '$1-$2-$3', '+18761234567');
I'm assuming that the +1
is constant and then use the \d
shortcut to match decimal characters. The value in {}
is the number of characters to match.
Also, there is a Regexp Library on the Internet. It may be of help.
Search for 'phone':
http://www.regexlib.com/Search.aspx?k=phone&c=-1&m=-1&ps=20
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With