Remove accents from an array

2

I have a select in which it is populated through a query in the database:

<select class="form-control" name="select_cidades">
                <option>Selecione a Cidade</option>
                <?php

                $result_cidades = "SELECT * FROM cidades";
                $resultado = mysqli_query($conn, $result_cidades);
                while($row_cidades = mysqli_fetch_assoc($resultado)){
                    ?>
                    <option value="<?php echo $row_cidades['id']; ?>"> <?php echo $row_cidades['nome']; ?>
                    </option>

                    <?php
                }

                ?>
            </select>

The problem is that cities that have accents are appearing symbol of? I could not find any function that wipes accents on an array. Anyone have any?

    
asked by anonymous 06.03.2018 / 19:26

1 answer

2

Here's a function I use:

function removerAcentos($string){
    return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/",
    "/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/",
    "/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"),
    explode(" ","a A e E i I o O u U n N"),$string);
}

And in your code you can do something like:

<option value="<?php echo $row_cidades['id']; ?>"> <?php echo removerAcentos($row_cidades['nome']);

But ideally you check the encoding of your HTML, instead of taking the accents try to use this function:

<option value="<?php echo $row_cidades['id']; ?>"> <?php echo utf8_encode($row_cidades['nome']);

Documentation: utf8_encode ()

    
06.03.2018 / 19:50