Problem in UTF-8 characters

Asked

Viewed 394 times

2

I have an error on my site, in the matter of UTF-8

If I use á it goes to link as %E3%83%E2%A1 and printa in input as %e3%83%e2%a1 in hosting.

But on localhost it goes as ã¡ and comes back as %c3%a3%c2%a1.

The codes I’m using are these:

To show the Code:

function mostrar($string) {
    $string = str_replace('+', ' ', $string);

    $string = utf8_decode($string);

    return mb_strtolower( strip_tags( trim( $string ) ) );
}

To Send the Code:

function limpar($string) {
        $string = str_replace(' ', '+', $string);

        $string = utf8_encode($string);

        return mb_strtolower( strip_tags( trim( str_replace('/', '', $string) ) ) );
    }

Sending pro header('Location')to appear in the site url:

if(isset($_POST['procm-p']) or isset($_POST['procm-s'])){

        $pesquisa = strip_tags($_POST['procm-s']);

        if(isset($pesquisa) and !empty($pesquisa)){

            $link = $surl.'p/'.limpar($pesquisa);

            header("Location: ".$link);

        }else{

            echo '<script>alert("Pesquisa Invalida");</script>';

        }

    }

Printando in input:

<input type="text" name="procm-s" value="<?php echo mostrar( $url[1] ); ?>" placeholder="Procurar Musicas" />

How is the form:

<div id="pesquisa2">
    <form action="" enctype="multipart/form-data" autocomplete="on" method="post" name="procm">
        <input type="text" name="procm-s" value="<?php echo mostrar($url[1]); ?>" placeholder="Procurar Musicas" />
        <input type="submit" value="Procurar" name="procm-p" id="btn" />
    </form> 
</div>

I put the website on the air so you can see: http://searchable/

What I wanted was for him to be like a utf-8 encoded and come back as utf-8 normal, but this does not happen.

And if you notice as I said above, it has an immense difference between my localhost and my hospedagem.

You know what? I’m using utf8_encode() and utf8_decode() and even header('Content-Type: text/html; charset=utf-8');.

1 answer

7


PHP already has specific functions to handle URL, which are urlencode() and urldecode(). Note that they already solve the exchange of + by space, as this is part of the encoding URL used by mime type application/x-www-form-urlencoded.

For the purposes of comparison, the rawurlencode() is similar, but does not have the same behavior with the sign of +. This already follows the RFC 3986.

It is also important to note that if your page is already in UTF-8, you should not use utf8_encode nor utf8_decode, because they are functions to change the encoding of an ISO-8859-1 string to UTF-8, and vice versa.

Links to the PHP manual:

Browser other questions tagged

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