UTF-8 character problem

2

With an error on my site, in% of UTF-8

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

But in localhost it goes as ã¡ and returns as %c3%a3%c2%a1 .

The codes I'm using are these:

To display the Code:

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

    $string = utf8_decode($string);

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

To Send Code:

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

        $string = utf8_encode($string);

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

Sending header('Location') to appear on 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>';

        }

    }

Printing in the 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 placed the site on the air so that you can see: link

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

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

Do you know what it can be? I am using utf8_encode() and utf8_decode() and even header('Content-Type: text/html; charset=utf-8'); .

    
asked by anonymous 16.04.2016 / 21:32

1 answer

6

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

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

It's 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 a string ISO-8859-1 for UTF-8, and vice versa.

Links to the PHP manual:

16.04.2016 / 22:31