Remove accents from an array

Asked

Viewed 689 times

2

I have a select in which it is completed through a bank query:

<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 ? I couldn’t find any function that cleans accents in an array. Does anyone have any?

1 answer

2


Follows 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 out the accents try using this function:

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

Documentation: utf8_encode()

  • The second one worked. I didn’t know I had to use Ncode in php, so I only declared in html it already worked accent ahahh thanks!

Browser other questions tagged

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