php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20934 htmlspecialchars returns latin1 from UTF-8
Submitted: 2002-12-11 06:58 UTC Modified: 2002-12-12 07:38 UTC
From: renato at cria dot org dot br Assigned:
Status: Closed Package: Strings related
PHP Version: 4CVS-2002-12-11 (dev) OS: Red hat linux 8.0
Private report: No CVE-ID: None
 [2002-12-11 06:58 UTC] renato at cria dot org dot br
I used the script bellow for testing (calling it from MS Internet Explorer to directly see the xml output).

Calling it without parameters one should see a simple xml document showing a string in latin1.

Calling it with "?charset=utf8", the script correctly converts the string from latin1 to UTF-8 but after using "htmlspecialchars" it goes back to latin1, and the xml becomes invalid. (put a comment on the "htmlspecialchars" line after the character conversion and the xml will show up in UTF-8 without problem).

<?php

$string = "Hello from S?o Paulo";

$charset = isset($_GET["charset"]) ? $_GET["charset"] : "latin1";

if ($charset == "utf8")
{
  $charset_code = "UTF-8";
 
  $show_string = mb_convert_encoding($string, "UTF-8", "ISO-8859-1"); 
  $show_string = htmlspecialchars($show_string, ENT_COMPAT, "UTF-8");
}
else
{
  $charset_code = "ISO-8859-1";

  $show_string = htmlspecialchars($string);
}

header ("Content-type: text/xml");

echo "<?xml version='1.0' encoding='$charset_code' ?>\n";

?>
<test>

<?php print($show_string); ?>

</test>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-12 07:10 UTC] moriyoshi@php.net
Thanks for your effort again.
Verified this behaviour.

 [2002-12-12 07:19 UTC] wez@php.net
Is this problem also in the PHP 4.3.0RC3?
If so, this should be resolved before release.
 [2002-12-12 07:38 UTC] moriyoshi@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

It's fortunate that I managed to fix this before the release.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC