特殊字符替换为?

时间:2017-11-04 23:05:36

标签: php html mysql

大家好我在我的代码中遇到了丢失字符的问题,尝试使用utf-8的一些东西,但没有用。整个网站是在PHP上,但其余的工作正常只是这个问题的页面。

<?php
      $tipo =$_GET["tipo"];

      $conexao = mysqli_connect("127.0.0.1","root","","db_tcc") or die ("Não foi possível se conectar com o servidor.");
      $varsql = "SELECT * FROM tb_guias WHERE TIPO=$tipo ORDER BY id ASC";
      $carregar_guias = mysqli_query($conexao, $varsql);




      while ($linha = mysqli_fetch_array($carregar_guias))
      {
        $titulo = $linha["TITULO"];
        $blob1 = $linha["IMG1"];
        $blob2 = $linha["IMG2"];
        $blob3 = $linha["IMG3"];
        $texto = $linha["TEXTO"];

        $img1 = imagecreatefromstring($blob1); 

        ob_start(); 
        imagejpeg($img1, null, 80);
        $data1 = ob_get_contents();
        ob_end_clean();

      $img2 = imagecreatefromstring($blob2); 

      ob_start(); 
      imagejpeg($img2, null, 80);
      $data2 = ob_get_contents();
      ob_end_clean();

      $img3 = imagecreatefromstring($blob3); 

      ob_start(); 
      imagejpeg($img3, null, 80);
      $data3 = ob_get_contents();
      ob_end_clean();


        echo "<div class='loc'><table class='tablel'><tr><td colspan='3'>$titulo</td></tr><tr><td>";
        echo '<img  src="data:image/jpg;base64,' .  base64_encode($data1)  . '" class="imag"  />';
       echo"</td><td>";
       echo '<img  src="data:image/jpg;base64,' .  base64_encode($data2)  . '" class="imag"  />';
       echo"</td><td>";
       echo '<img  src="data:image/jpg;base64,' .  base64_encode($data3)  . '" class="imag"  />';
       echo "</td></tr><tr><td colspan='3'>$texto</td></tr></table></div>";
        echo "<br><hr><br>";

      }
    ?>

1 个答案:

答案 0 :(得分:0)

首先,您需要在页眉添加中为HTML output..指定字符集:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
您可以使用PHP中的

(此行必须是您代码中的第一行):

<?php header("Content-type: text/html; charset=utf-8");?>

然后你需要在你网站的根目录中写一个.htaccess

`# Set httpd charset to utf-8 
 AddDefaultCharset On
 AddDefaultCharset utf-8`

将php charset设置为utf-8并设置mbstring(您可能需要安装mbstring模块)

php_value default_charset utf-8
php_value mbstring.internal_encoding utf-8
php_value mbstring.func_overload 7

source

然后尝试添加htmlentities功能

你的输出就像:

  echo '<img  src="data:image/jpg;base64,' .  htmlentities(utf8_encode($data1), 0, "UTF-8")  . '" class="imag"  />';
   echo"</td><td>";
   echo '<img  src="data:image/jpg;base64,' .  htmlentities(utf8_encode($data2), 0, "UTF-8")  . '" class="imag"  />';
   echo"</td><td>";
   echo '<img  src="data:image/jpg;base64,' .  htmlentities(utf8_encode($data3), 0, "UTF-8")  . '" class="imag"  />';