htmlentities for array elements

0

Good day! I'm putting together an app that has a news section that takes it from a database. Since in the database (which was already made) the texts have several special characters that I want to clean, for which I used htmlentities ($ string) However, when I do that the text disappears.

Without using htmlentities, it looks like this

  

According to Decree 49/2014 published in the Official Bulletin, the list of pathologies that can be produced by the

When I use it, this is deleted.

I show you the code because maybe something is wrong Thanks!

      <?PHP

       /*  include('lib/helper/ArticleTextExtractor.php'); */

       $i = 1;

       $SQL_NEWS=" SELECT titulo, copete, cuerpo, imagen, id FROM novedad 
    ORDER BY orden asc LIMIT 3";

    $QUERY_NEWS=mysql_query($SQL_NEWS);

    WHILE($NEWS = mysql_fetch_row($QUERY_NEWS)){
        // Remove HTML code from the "Cuerpo" field and leave only 
    paragraphs with article's plain text.
        $NEWS_FLAT = extractArticleText($NEWS[2]); */

      ?>
      <div class="col-sm-3 notaHome">
       <img src="/web/uploads/novedad/<?php echo $NEWS[3] ?>" class="img-
       thumbnail" alt="">
      <h4 class="azul tituloNota"><?php echo htmlentities($NEWS[0]) ?></h4>
      <hr class="azul">
      <h5 class="subtituloPrincipal subtituloNota"><?php echo 
     htmlentities($NEWS[1]) ?>.</h5>
      <?php echo ($NEWS[2]) ?>
      <a href="novedadesDetalle.html" id="leerMas" role="button" class="btn 
    btn-primary">SEGUIR LEYENDO <span class="glyphicon glyphicon-chevron-
    right"></span></a>
    </div>
      <?php

      $i++;

      }

    ?>

The code that is commented, I did not. Anyway, call an extractArticleText function that is the following:

    <?php
      function extractArticleText($htmlString) 
      {
       $replaced = preg_replace("/<a(.|\n)*?>(.|\n)*?<\/a>/", "", $htmlString); // remove anchors
       $replaced = preg_replace("/<(.|\n)*?>|(&nbsp;)/", "", $replaced); 
      //remove HTML tags and &nbsp;
     return $replaced;
    }
    ?>

Thank you very much !!

    
asked by Maria Laura 15.02.2018 в 14:50
source

2 answers

0

You must set the character set in the connection to the database for example (PDO):

$base=new PDO('mysql:host=localhost; dbname=asocacion_db', 'root', '');

$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$base->exec("SET CHARACTER SET UTF8"); //Estableciendo juego de caracteres

with Mysqli;

mysqli_query("SET NAMES 'utf8'");  

Another way is to put before any output or label html in the header of your document php a header like this;

header("Content-Type: text/html;charset=utf-8");

You can also add the use of characters in the meta tag

<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
    
answered by 15.02.2018 / 14:58
source
0

You have to specify the utf-8 coding in the third method parameter htmlentities so that show the accents correctly. For example:

<?php echo htmlentities($NEWS[0], ENT_QUOTES, "UTF-8") ?>
    
answered by 15.02.2018 в 14:58