Text returning with "?" instead of quotation marks and hyphens

Asked

Viewed 529 times

1

Good afternoon.

My site runs on ISO-8859-1 encoding and when I pull data from an external XML, some characters return as a question mark.

I tried to convert with <?php header("Content-type: text/html; charset=utf-8"); ?> but all the rest of the site like the top, footer and other areas suffered from the exchange of coding.

I also added utf8_decode() and nothing solved.

Does anyone know any solution to the problem?

An example link to https://api.dino.com.br/v2/news/118219/mundo-do-marketing

<?php
                $arquivo = "https://api.dino.com.br/v2/news/$id/mundo-do-marketing";

                $info = file_get_contents($arquivo);

                $lendo = json_decode($info);

                echo '<pre>' . print_r($lendo, true) . '</pre>';

                echo $lendo->Item->Summary;

                $titulo = utf8_decode($lendo->Item->Title);
                $autor = utf8_decode($lendo->Item->Author);
                $retranca = utf8_decode($lendo->Item->Summary);
                $data = date('d/m/Y', strtotime($lendo->Item->PublishedDate));
                $categoria = utf8_decode($lendo->Item->Categories[0]->Name);
                $conteudo = utf8_decode($lendo->Item->Body);

                if($id == 108988){
                    redirect(base_url('noticias-corporativas'));
                }

                else{
            ?>

                    <h2><?php echo $titulo; ?></h2>
                    <p class="retranca-dino"><?php echo $retranca; ?></p>
                    <p class="autor-dino">Categoria: <span class="autor"><?php echo $categoria; ?></span></p>
                    <p class="autor-dino">Autor: <span class="autor"><?php echo $autor; ?></span></p>
                    <span class="data-dino">Data de Publicação: <span style="font-weight: normal;"><?php echo $data; ?></span></span>

                <?php
                        if(empty($lendo->Item->Image->Url)){
                            echo NULL;
                        }

                        else{

                            $imagem = $lendo->Item->Image->Url;
                ?>
                        <div class="imagem-dino">
                            <img class="imagem" src="<?php echo $imagem; ?>" alt="<?php echo $titulo; ?>" title="<?php echo $titulo; ?>" />
                        </div>

                <?php
                        }
                    }
                ?>

                <div class="conteudo-dino"><?php echo $conteudo; ?></div>

            <?php
                echo br(1);
            ?>

Att.

  • have tried mb_convert_encoding($texto, 'ISO-8859-1', 'UTF-8')?

  • deoliveiralucas did not work, keeps returning as interrogation.

  • 1

    How do you get this external XML data? Put the question there.

  • I updated the question

  • One thing I noticed is that the shape of the quotes are different, let me try to explain. Kind of an hour are rounded and another time is normal as on the keyboard, will be that are not registering content with mixed text?

2 answers

0

Following the PHP documentation you should use the utf8_encode function. Look at the documentation: utf8_encode - Encode an ISO-8859-1 string for UTF-8. See if it solves.

  • I had already done this and keeps giving the same problem. One thing I noticed is that the shape of the quotes are different, let me try to explain. ?

  • I saw there that you used json_decode(). You already tried to use the simplexml_load_file function() ?

  • I’ve tried my mistake

0

I faked your mistake here. What is probably happening is that even though your site is running in ISO, file_get_contents is already getting the UTF-8 encoding.

The three alternatives to tidy up:

1.Take the utf8_decode:

$conteudo = $lendo->Item->Body;

2.Add the first one (excluding utf8_decode) by adding a:

ini_set( 'default_charset', 'utf-8');
  1. Just add that line (keep the rest as it is in your code):

    ini_set( 'default_charset', 'iso-8859-1');

  • I add this line ini_set( 'default_charset', 'iso-8859-1'); in which part of the code? Before the $title variable?

  • I added this line at the beginning of my file, one of the problems that happens is that it reverses the problem to other areas of the page. Other special characters that rotated in the ISO now have a question mark inside a black diamond

  • Tried the first alternative?

  • Yes Rodrigo, I tried the 3 solutions. The problem that other parts of the page have broken characters.

  • If the problem reverses a workarround, give an ini_set again when finished.

Browser other questions tagged

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