PHP Character Encoding Problems

PHP character encoding problems

header('Content-type: text/html; charset=UTF-8') ;

/**
* Encodes HTML safely for UTF-8. Use instead of htmlentities.
*
* @param string $var
* @return string
*/
function html_encode($var)
{
return htmlentities($var, ENT_QUOTES, 'UTF-8');
}

Those two rescued me and I think it is now working. I'll come back if I continue to encounter problems. Should I store it in the DB, eg as "&" or as "&"?

UTF8 Encoding problem - With good examples

This may be a job for the mb_detect_encoding() function.

In my limited experience with it, it's not 100% reliable when used as a generic "encoding sniffer" - It checks for the presence of certain characters and byte values to make an educated guess - but in this narrow case (it'll need to distinguish just between UTF-8 and ISO-8859-1 ) it should work.

<?php
$text = $entity['Entity']['title'];

echo 'Original : ', $text."<br />";
$enc = mb_detect_encoding($text, "UTF-8,ISO-8859-1");

echo 'Detected encoding '.$enc."<br />";

echo 'Fixed result: '.iconv($enc, "UTF-8", $text)."<br />";

?>

you may get incorrect results for strings that do not contain special characters, but that is not a problem.

Character encoding issues PHP

This is almost definitely an issue with the charset. If you're using Apache you can force charset utf-8 in your .htaccess:

AddDefaultCharset utf-8

Alternatively, you can also set the charset in PHP

header('Content-Type: text/html; charset=UTF-8');

Note that header() must come before any output.

Character encoding issue with PHP/MySQL

(Since this clearly shows a PDO omission, I am reopening)

One-line change:

$pdo = new 
PDO("mysql:host=$servername;dbname=$dbname;dbname=db;charset=utf8mb4'",
$username, $password);

References:

http://mysql.rjweb.org/doc.php/charcoll#php

Trouble with UTF-8 characters; what I see is not what I stored (Look especially for "black diamond".)

UTF-8 encoding problems

SOLVED:

In my connection script I changed this:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass);
$dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');

To this:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

and all works as it should.

Thanks for looking

PHP and character encoding problem with  character

I use this:

function replaceSpecial($str){
$chunked = str_split($str,1);
$str = "";
foreach($chunked as $chunk){
$num = ord($chunk);
// Remove non-ascii & non html characters
if ($num >= 32 && $num <= 123){
$str.=$chunk;
}
}
return $str;
}

PHP UTF8 encoding issue with Special characters

utf8_decode is what is changing the characters into "?". You shouldn't do that - just use the data as is without processing it. Use the option JSON_UNESCAPED_UNICODE on json_encode and it should work.

json_encode(array('result'=>$str), JSON_UNESCAPED_UNICODE);

UTF-8 encoding issue in php, special characters

The string has UTF-8 characters. You need to either decode them, or tell whatever is reading it to use UTF-8 encoding.

So, this:

$pre_subject = (strip_tags(html_entity_decode($temp_subject)));
$pre_subject = str_replace('Â', '', $pre_subject); // if this was trying to fix the problem, remove it
$pre_subject = utf8_decode($pre_subject);

Or add this to <head>:

<meta charset="utf-8">

PHP output encoding issues with UTF-8 strings from MySQL databases

You should not change all the character_set% fields, just the three that are affected by SET NAMES utf8;.

Don't use utf8_encode or decode.

You have probably messed up when storing.

This seems to recover the characters, but this not a viable fix:

CONVERT(CAST(CONVERT('pürson from “Vancouver, Canadaâ€' USING latin1)
AS BINARY)
USING utf8)
--> 'pürson from “Vancouver, Canada - spec',

In order to figure out what was done, please provide

SELECT col, HEX(col) FROM tbl WHERE ...

for some cell that is not rendering properly.



Related Topics



Leave a reply



Submit