With the ninth digit on the phones, how can I solve the problem of displaying 8-digit phones?

Asked

Viewed 833 times

5

This was a question that came to me with the news of the ninth digit in the phones here in Brazil.

I have in the system a list of phones that are saved along with the DDD.

How could I do for an 8-digit phone (and with the DDD) can be filled with the 9?

Example of how it is saved in the database:

(31)9915-2855

How could I turn it into (31)99915-2855

Observing: I accept solutions in both PHP and Mysql.

  • 1

    Just remember that not all phones on the hottie already have the ninth digit. =)

  • 1

    But in Minas Gerais it looks like it will. So it will have one If gambiarra

3 answers

9

You can use this instruction

UPDATE telefone 
SET celular = Replace (celular , '(31)', '(31)9')
WHERE celular REGEXP '\\(*31\\)*9[[:alnum:]]{3}-*[[:alnum:]]{4}$';    

thus avoids updating phone numbers that are not cell phone numbers and that perhaps already contain the ninth digit

The WHERE statement captures numbers whether or not masked, but the UPDATEonly works if the value is with (31)9 (no space between ')' and 9)

To update numbers stored in other formats modify the REPLACE

8


If all numbers are in this format, it is possible to use substr_replace() to add the 9 in the fifth position of the string.

<?php
    $str = '(31)9915-2855';
    $novo = substr_replace($str, '9', 5, 0);
    echo $novo;

Exit:

(31)99915-2855
  • 2

    +1 You’re the guy. I’ve never used this function in my PHP life!

  • After a if (strlen($telefone) == 14) can do this, to validate whether it is new or old - gambiarra

  • @Wallacemaxters, can solve with substr() or transform the string into an array, the best would be if ddd were separate from the number, ai is only to use str_pad().

7

At bank level, and only for telephones in Minas, we can make the following solution:

UPDATE telefone 
SET    telefone = ( Replace (telefone, '(31)', '(31)9') )

Assuming, of course, that the camp telefone be a string, and that all phones have the same format. Ah, and that the field is called telefone even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.