There is a small function that processes the phone number before the output, the problem is that the function incorrectly displays the phone, for example, the number is recorded 9061755555, then it displays in the format (906) 175-55-55. Ukrainian, Russian ... How to add a function so that it substitutes +3 if it is not there or +7 generally formatted the phone beautifully ...

function echoNumber($aphone) // номер в формате (xxx) xxx-xx-xx { $sArea = substr($aphone, 0, 3); $sNumber1 = substr($aphone, 3, 3); $sNumber2 = substr($aphone, 6, 2); $sNumber3 = substr($aphone, 8, 2); $aphone = "(" . $sArea . ") " . $sNumber1 . "-" . $sNumber2 . "-" . $sNumber3; return ($aphone); } 
  • "In general, the phone formatted beautifully" - this is programming, there is no "beautifully", here only formal definitions of algorithms with a deterministic conclusion appear. - etki
  • What does it mean "incorrectly displays the phone" in the first sentence, if it should output "a number in the format (xxx) xxx-xx-xx", if it does output it exactly like that? - Timur Musharapov

2 answers 2

libphonenumber for php

 $swissNumberStr = "9061755555"; $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); try { $swissNumberProto = $phoneUtil->parse($swissNumberStr, "RU"); echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL); } catch (\libphonenumber\NumberParseException $e) { var_dump($e); } 

Demo

  • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky
  • @NicolasChabanovsky honestly, I don’t know what can be added here except - "You can try the library ..., it works something like this:" Well, you can learn everything else from the library documentation. I would be grateful if you can add theses here. - Bookin
 function echoNumber($aphone) // номер в формате (xxx) xxx-xx-xx { $lengthOfString = strlen($aphone); $codeOfCountry = ""; if($lengthOfString > 10 && stristr($phone, '+') === false) { if (substr($phone, 0, 1) == 8) { $codeOfCountry = "+7"; } else { $codeOfCountry = "+" . substr($phone, 0, $lengthOfString-10); } } else { $codeOfCountry = "+7"; } $sArea = substr($aphone, 0, 3); $sNumber1 = substr($aphone, 3, 3); $sNumber2 = substr($aphone, 6, 2); $sNumber3 = substr($aphone, 8, 2); $aphone = $codeOfCountry . "(" . $sArea . ") " . $sNumber1 . "-" . $sNumber2 . "-" . $sNumber3; return $aphone; } 

If it is to rewrite, then so (and if numbers of one format are stored in the database). In general, for these purposes it is better to use JS, which would already bring the phone number into the desired form, adding checks - the benefit is there are suitable libraries.

  • well, why write a function in a function and forget about the brackets {} - Naumov
  • And really corrected. - Timur Musharapov
  • so more compact and visible, but still you have not taken into account that the phone is stored without a country code. - Naumov
  • something went wrong ... it was gyazo.com/c5f9cb9d92f00e0083c0dcaf35c2dd53 became gyazo.com/64fdb2a1be69144ef5897b29fb97ee38 - Alexander Sizintsev
  • This is your substr divides, it is designed only for 10-digit numbers ($ sArea, $ sNumber1, $ sNumber2, $ sNumber3) .. - Timur Musharapov