Guest User

Untitled

a guest
Apr 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. <?php
  2. function formataTelefone($numero){
  3. if(strlen($numero) == 10){
  4. $novo = substr_replace($numero, '(', 0, 0);
  5. $novo = substr_replace($novo, '9', 3, 0);
  6. $novo = substr_replace($novo, ')', 3, 0);
  7. }else{
  8. $novo = substr_replace($numero, '(', 0, 0);
  9. $novo = substr_replace($novo, ')', 3, 0);
  10. }
  11. return $novo;
  12. }
  13.  
  14. $str = '1122223333';
  15. echo formataTelefone($str);
  16.  
  17. (11)922223333
  18.  
  19. static public function masc_tel($TEL) {
  20. $tam = strlen(preg_replace("/[^0-9]/", "", $TEL));
  21. if ($tam == 13) { // COM CÓDIGO DE ÁREA NACIONAL E DO PAIS e 9 dígitos
  22. return "+".substr($TEL,0,$tam-11)."(".substr($TEL,$tam-11,2).")".substr($TEL,$tam-9,5)."-".substr($TEL,-4);
  23. }
  24. if ($tam == 12) { // COM CÓDIGO DE ÁREA NACIONAL E DO PAIS
  25. return "+".substr($TEL,0,$tam-10)."(".substr($TEL,$tam-10,2).")".substr($TEL,$tam-8,4)."-".substr($TEL,-4);
  26. }
  27. if ($tam == 11) { // COM CÓDIGO DE ÁREA NACIONAL e 9 dígitos
  28. return "(".substr($TEL,0,2).")".substr($TEL,2,5)."-".substr($TEL,7,11);
  29. }
  30. if ($tam == 10) { // COM CÓDIGO DE ÁREA NACIONAL
  31. return "(".substr($TEL,0,2).")".substr($TEL,2,4)."-".substr($TEL,6,10);
  32. }
  33. if ($tam <= 9) { // SEM CÓDIGO DE ÁREA
  34. return substr($TEL,0,$tam-4)."-".substr($TEL,-4);
  35. }
  36. }
  37.  
  38. echo self::masc_tel('5512123456789');
Add Comment
Please, Sign In to add comment