JuanValdez
JuanValdez

Reputation: 13

PHP 5.5 with html_entity_decode and IIS8

We recently moved from IIS 7.5 with PHP 5.3.4 to IIS 8 with PHP 5.5. We are having an issues with data pulled from MySQL. The issue was not present on the previous infrastructure.

When a string is pulled from MySQL and displayed to the client the encoding is not displaying properly.

Ex:

Text should display as and does on old infrastructure:

"¿Qué planearon Sandra y Carlos en este episodio?

But it displays as:

"¿Qué planearon Sandra y Carlos en este episodio?"php

I have tried the following:

  1. Add header to client interprets all as UTF-8, no change.

    header('content-type: text/html; charset=utf-8');

  2. echo the encoded characters below: (they display correctly to client as: ä ö ü ß €.)

    "\xc3\xa4"."\xc3\xb6"."\xc3\xbc"."\xc3\x9f"."\xe2\x82\xac";

  3. Add encoding options to html_entity_decode for UTF-8. No Change.

Any ideas? Code blow.

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
// CONNECT TO THE DATABASE
$DB_NAME = 'removed';
$DB_HOST = 'removed';
$DB_USER = 'removed';
$DB_PASS = 'removed';

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT * FROM `tablename` WHERE `item`='1234'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

// GOING THROUGH THE DATA
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    echo html_entity_decode($row['prompt']);    
    }
}
else {
    echo 'NO RESULTS';  
}

// CLOSE CONNECTION
mysqli_close($mysqli);

Upvotes: 1

Views: 973

Answers (2)

JuanValdez
JuanValdez

Reputation: 13

Pascalc beat me to it:


It appears the default encoding for HTML_entities_decode changed after php 5.4 ISO-8559-1 to UTF-8. So content that was put into MySQL with old PHP was done so as ISO-8859-1. The below did the trick for me.

html_entity_decode($row['prompt'], ENT_QUOTES, 'ISO-8859-1');

Upvotes: 0

Pascalc
Pascalc

Reputation: 575

Looks like your database is stored in Latin 1 encoding and not in UTF8, the quick fix should be to use this syntax:

<?php
echo html_entity_decode($row['prompt'],ENT_COMPAT | ENT_HTML401,'ISO-8859-1');

The real fix would be to fix your database to store data in UTF8 and have all of your toolchain in UTF8.

html_entity_decode() has UTF8 as default encoding since PHP 5.4: https://www.php.net/manual/en/migration54.other.php

Upvotes: 1

Related Questions