Phone Number Beautifier


/ Published in: PHP
Save to your folder(s)

Built for german numbers.


Copy this code and paste it in your HTML
  1. /*
  2.  * Example:
  3.  *
  4.  * 0049 1136 - 180-0 +49 1136 1800
  5.  * +49-89-66878-6402 +49 8966 8786 402
  6.  * 0049 2223 34-1325 +49 2223 341325
  7.  * +49 (0)88 99147687 +49 8899 147687
  8.  */
  9.  
  10. function phone_number($phone)
  11. {
  12. $steps = 5;
  13.  
  14. $phoneBAK = $phone = preg_replace("/[^0-9+]/", '', str_replace(array(' ','(0)'),'', $phone));
  15. while($phone[0]==0) $phone = substr($phone, 1);
  16. if(substr($phone, 0, 2)==49) {
  17. $phone = "+".substr($phone, 0, 2)." ".substr($phone, 2);
  18. $start = $steps+3;
  19. }
  20. else {
  21. $phone = $phoneBAK;
  22. $start = $steps;
  23. }
  24. for($i=$start;$i<strlen($phone);$i=$i+$steps) $phone = substr_replace($phone, ' ', $i,0);
  25.  
  26. $end = strrchr($phone, ' ');
  27. if(strlen($end)<4) $phone = substr($phone, 0, 0-strlen($end)) . substr($phone, 0-strlen($end)+1);
  28.  
  29. return $phone;
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.