<?php

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

$foo = "ヨコノトÂÈÄfun";

function
out($code)
{
    global
$foo;
    eval(
'$x = ' . $code . ';');
    print
'<h3>' . htmlspecialchars($code) . '</h3>' . "\n";
    print
'<p>Result: ' . $x . '</p>' . "\n";
    print
'<p>Hex: ' . bin2hex($x) . '</p>'. "\n";
}

function
utf8ToHtmlOrd($x) {
    return
preg_replace_callback('/./u', '__utf8CharToHtmlOrd', $x);
}

function
__utf8CharToHtmlOrd($x)
{
    
$x = unpack('H*', mb_convert_encoding($x[0], 'UTF-16', 'UTF-8'));
    return
'&#' . hexdec($x[1]) . ';';
}

$GLOBALS['uiocHtmlCodes'] = array(
    
"\""=>"&quot;",
    
"©"=>"&copy;",
    
"®"=>"&reg;",
    
"÷"=>"&divide;",
    
"™"=>"&trade;",
    
"&"=>"&amp;",
    
"<"=>"&lt;",
    
">"=>"&gt;",
    
"ˆ"=>"&circ;",
    
"~"=>"&tilde;",
    
"‘"=>"&lsquo;",
    
"’"=>"&rsquo;",
    
"‚"=>"&sbquo;",
    
"”"=>"&rdquo;",
    
"€"=>"&euro;"
    
);

function
brynEscape($x)
{
    return
strtr( $x, $GLOBALS['uiocHtmlCodes'] );
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>PHP htmlentities() + UTF-8 considered harmful</title>
</head>
<body>
<h1>PHP htmlentities() + UTF-8 considered harmful</h1>
<?php out('$foo') ?>
<?php out
('htmlspecialchars($foo)') ?>
<?php out
('htmlspecialchars($foo, ENT_QUOTES, \'UTF-8\')') ?>
<font color="red">
<?php out('htmlentities($foo)') ?>
</font>
<?php out('htmlentities($foo, ENT_QUOTES, \'UTF-8\')') ?>
<?php out
('brynEscape($foo)') ?>
<?php
//out('utf8ToHtmlOrd($foo)') ?>
<hr />
<p>See <a href="http://www.phpwact.org/php/i18n/utf-8">http://www.phpwact.org/php/i18n/utf-8</a>
    for more information.</p>
<p><a href="source.php">Source</a></p>
<a href="..">More <em>Play</em></a>
</body>
</html>