-1

I have string with phone number that I need to transform from one format to another.

Example:

"02030266079" => "0203 026 6079"

I tried:

$num = "02030266079";
$transformed= preg_replace("/^(/d{4})(/d{3})(/d{4})$/", "$1 $2 $3", $num);

But nothing happens!

8
  • 1
    Also, as a starting point you have to define clear rules how the transformation should be done. An example is not enough here. Especially since most likely one requires knowledge about where to place those spaces. Knowledge about what part of the sequence is an area code and the like. This is not trivial.
    – arkascha
    May 27, 2015 at 9:28
  • 1
    Read this stackoverflow.com/questions/4708248/…
    – user4294557
    May 27, 2015 at 9:28
  • Start by explaining the pattern that defines the format - eg. InternationalDialingCode+LocalDiallingCode+Number
    – foxbeefly
    May 27, 2015 at 9:28
  • Sorry. I'm new to this site and I promise to ask better questions next time. May 27, 2015 at 9:31
  • 1
    @user2546940 no prob, next time you should come with attempts. Here preg_replace('~(?<=^\d{4})|(?=\d{4}$)~', ' ', $str); is the answer for this question. May 27, 2015 at 9:32

1 Answer 1

2
$number = "12345678901";
$formatted_number = preg_replace("/^(\d{4})(\d{3})(\d{4})$/", "$1 $2 $3", $number);

It is php code and well tested, It works perfectly

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.