PHP output showing little black diamonds with a qu

2020-01-23 04:04发布

I'm writing a php program that pulls from a database source. Some of the varchars have quotes that are displaying as black diamonds with a question mark in them (�, REPLACEMENT CHARACTER, I assume from Microsoft Word text).

How can I use php to strip these characters out?

21条回答
Melony?
2楼-- · 2020-01-23 04:10

To make sure your MYSQL connection is set to UTF-8 (or latin1, depending on what you're using), you can do this to:

$con = mysql_connect("localhost","username","password");    
mysql_set_charset('utf8',$con);

or use this to check what charset you are using:

$con = mysql_connect("localhost","username","password");   
$charset = mysql_client_encoding($con);
echo "The current character set is: $charset\n"; 

More info here: http://php.net/manual/en/function.mysql-set-charset.php

查看更多
▲ chillily
3楼-- · 2020-01-23 04:10

This will help you. Put this inside <head> tag

<meta charset="iso-8859-1">
查看更多
来,给爷笑一个
4楼-- · 2020-01-23 04:14

I chose to strip these characters out of the string by doing this -

ini_set('mbstring.substitute_character', "none"); 
$text= mb_convert_encoding($text, 'UTF-8', 'UTF-8');
查看更多
Evening l夕情丶
5楼-- · 2020-01-23 04:15

Just add these lines before headers.

Accurate format of .doc/docx files will be retrieved:

 if(ini_get('zlib.output_compression'))

   ini_set('zlib.output_compression', 'Off');
 ob_clean();
查看更多
姐就是有狂的资本
6楼-- · 2020-01-23 04:18

I also faced this � issue. Meanwhile I ran into three cases where it happened:

  1. substr()

    I was using substr() on a UTF8 string which cut UTF8 characters, thus the cut chars could not be displayed correctly. Use mb_substr($utfstring, 0, 10, 'utf-8'); instead. Credits

  2. htmlspecialchars()

    Another problem was using htmlspecialchars() on a UTF8 string. The fix is to use: htmlspecialchars($utfstring, ENT_QUOTES, 'UTF-8');

  3. preg_replace()

    Lastly I found out that preg_replace() can lead to problems with UTF. The code $string = preg_replace('/[^A-Za-z0-9ÄäÜüÖöß]/', ' ', $string); for example transformed the UTF string "F(×)=2×-3" into "F � 2� ". The fix is to use mb_ereg_replace() instead.

I hope this additional information will help to get rid of such problems.

查看更多
戒情不戒烟
7楼-- · 2020-01-23 04:20

As mentioned in earlier answers, it is happening because your text has been written to the database in iso-8859-1 encoding, or any other format.

So you just need to convert the data to utf8 before outputting it.

$text = “string from database”;
$text = utf8_encode($text);
echo $text;
查看更多
登录 后发表回答