Error in CEP query in PHP

I am trying to fix in PHP the URL that queries the ZIP code, when typing the ZIP code XXXXX-XXX The URL is showing the characters amp; that are causing error in the XML result.

Correct query:

http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=88160-282

Query with error:

http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=88160-282

How to remove amp; characters from the query?

    // FUNÇAO PARA CONSULTAR CEP

public function consultarCep() {
    $cep = $_POST['cep'];

    $reg = simplexml_load_file("http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=" . $cep);

    $dados['sucesso'] = (string) $reg->resultado;
    $dados['endereco'] = (string) $reg->tipo_logradouro . ' ' . $reg->logradouro;
    $dados['bairro'] = (string) $reg->bairro;
    $dados['cidade'] = (string) $reg->cidade;
    $dados['estado'] = (string) $reg->uf;

    echo json_encode($dados);
}
Author: aleestevao, 2018-09-20

1 answers

You can use the function html_entity_decode.

Example:

$var = "http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=88160-282";

echo html_entity_decode($var);
 -1
Author: MikeDoouglas, 2018-09-20 15:06:31