With the 9th digit on the phones, how can I solve the problem of displaying phones that have 8 digits?

5

This was a question that came up with the ninth digit news on the phones here in Brazil.

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

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

Example of how it is saved in the database:

(31)9915-2855

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

Note : I accept both PHP and MySQL solutions.

    
asked by anonymous 15.10.2015 / 15:13

3 answers

8

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

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

Output:

(31)99915-2855
    
15.10.2015 / 15:25
9

You can use this statement

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

This prevents you from updating phone numbers that are not cell numbers and may already contain the ninth digit

The WHERE statement captures numbers that contain or not a mask, but the UPDATE only works if the value is (31) 9 (without space between ')' and 9 )

To update numbers stored in other formats, modify REPLACE

    
15.10.2015 / 17:12
7

At the bank level, and only for Minas phones, you can do the following solution:

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

Assuming, of course, that the telefone field is a string, and that all phones have the same format. Oh, and the field is called telefone itself.

    
15.10.2015 / 15:33