Christian
Christian

Reputation: 612

htmlentities and htmlspecialchars refuse to process string

Since the last update to PHP5 5.4.0-3 on my Debian box I noticed that some pages have empty fields where text from the MySQL database should be.

I played around a bit and found the problem.

<?php
$scselect = mysql_query("SELECT `name` FROM `forum_threads` WHERE `forum` = '1' ORDER BY `timestamp` DESC") or exit((mysql_error()));
    while ($scrow=mysql_fetch_array($scselect))
    {
        var_dump($scrow['name']);
        var_dump(htmlentities($scrow['name']));
    }
?>

strangely this is whats printed:

string(18) "php hu3: the Forum"
string(0) ""
string(18) "php hu2 score-rule"
string(0) ""
string(6) "php hu"
string(0) ""
string(15) "HU 8: Binarycnt"
string(0) ""

but if I use htmlentities with hardcoded content -> htmlentities("test"); it works like charm. Also if I do this:

var_dump("a".$scrow['name']);

it also says

string(0) ""

But it gets stranger. If I use htmlentities or htmlspecialchars with any other variable from the database it works just perfectly.

var_dump(htmlspecialchars($scrow['ID'])); // prints for example string(2) "87"

what can be the cause of this?

Upvotes: 4

Views: 569

Answers (3)

user1581659
user1581659

Reputation: 31

Recommended approach is to hard code the character set each time you use htmlentities.

This is a feature of the htmlentities call, and not affected by the default character set. migration54

Upvotes: 2

Stefan
Stefan

Reputation: 2068

the cause of this can be another encoding returned from the database (like UTF8)

so play a little around with utf8_encode($string) or utf8_decode($string)

another approach would be playing around with the encoding argument of htmlentities:

https://www.php.net/htmlentities

Upvotes: 1

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Try this :

htmlentities($scrow['name'], ENT_QUOTES | ENT_IGNORE, "UTF-8");

Upvotes: 3

Related Questions