Htmlentities and htmlspecialchars refuse to process string

Since the last update to PHP5 5.4.0-3 in 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']));
    }
?>

      

strange is what is 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 a charm. Also if I do this:

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

      

he also says

string(0) ""

      

But this is getting weirder. If I use htmlentities or htmlspecialchars with any other variable from the database it works fine.

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

      

what could be the reason for this?

+3


source to share


3 answers


Try the following:



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

      

+2


source


The recommended approach is to hard-code the character set every time you use htmlentities.



This is a function to call htmlentities and is independent of the default character set. migration54

+2


source


the reason for this might be a different encoding returned from the database (e.g. UTF8)

so play around with utf8_encode($string)

orutf8_decode($string)

another approach would play with the htmlentities encoding argument:

http://de2.php.net/htmlentities

+1


source







All Articles