I am trying to figure out how to convert a phone number to a format+18761234567in876-123-4567using a Regex replacement.
I think this should work
preg_replace('/^\+1(\d{3})(\d{3})(\d{4})$/i', '$1-$2-$3', '+18761234567');
I assume +1 is a constant, and then use the \d shortcut to match decimals. The value in {} is the number of characters to match.
+1
\d
{}
There is also a Regexp library on the Internet. This may be helpful.
Search for "phone":
http://www.regexlib.com/Search.aspx?k=phone&c=-1&m=-1&ps=20
I know this is not a regex, but it also works :-)
$num = '+18761234567'; $formatted = substr($num, 2, 3).'-'.substr($num, 5, 3).'-'.substr($num, 8);
This suggests that there are two characters - the beginning to ignore.
preg_replace("/([0-9a-zA-Z+]{2})([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$2-$3-$4", '+18761234567');
Source: https://habr.com/ru/post/890636/More articles:How does PHP make money? They donβt ask for donations or sell pro solutions - phpHow to configure session hash in setup for tests in Rails 3? - httpCreating and managing 3D matrices in Matlab - matrix-multiplicationIs there a way to programmatically install ApartmentState in STA? - .netList of sitemaps and sitemap files in robots.txt file? - robots.txthttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/890637/git-simplest-way-to-share-a-repository-between-two-laptops-on-a-local-network&usg=ALkJrhhcmNgsasLv8VKBuNyxxAJZ5Or1Zwget physical monitor size - c #Using ASP.Net tags in a .js file? - javascriptDynamic text in div - jqueryError deploying SharePoint 2010 solution package - SharePoint 2010 - Visual Studio 2010 - visual-studio-2010All Articles