Skip to content

Instantly share code, notes, and snippets.

@joko-wandiro
Last active September 29, 2023 08:32
Show Gist options
  • Save joko-wandiro/f5c935708d9c37d8940b to your computer and use it in GitHub Desktop.
Save joko-wandiro/f5c935708d9c37d8940b to your computer and use it in GitHub Desktop.
HTML entities and HTML special characters
<?php
error_reporting(E_ALL);
$charsets = array(
"ISO-8859-1" => "Western European, Latin-1. ",
"ISO-8859-5" => "Little used cyrillic charset (Latin/Cyrillic). ",
"ISO-8859-15" => "Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1). ",
"UTF-8" => "ASCII compatible multi-byte 8-bit Unicode. ",
"cp866" => "DOS-specific Cyrillic charset. ",
"cp1251" => "Windows-specific Cyrillic charset. ",
"cp1252" => "Windows specific charset for Western European. ",
"KOI8-R" => "Russian. ",
"BIG5" => "Traditional Chinese, mainly used in Taiwan. ",
"GB2312" => "Simplified Chinese, national standard character set. ",
"BIG5-HKSCS" => "Big5 with Hong Kong extensions, Traditional Chinese. ",
"Shift_JIS" => "Japanese Shift_JIS",
"EUC-JP" => "Japanese EUC-JP",
"MacRoman" => "Charset that was used by Mac OS. ",
);
header('content-type: text/plain; charset=ISO-8859-15');
foreach ($charsets as $charset => $description) {
$htmlTransitionTableHTMLEntities = get_html_translation_table(HTML_ENTITIES, ENT_COMPAT | ENT_QUOTES, $charset);
$listTableHTMLEntities = implode(",", array_flip($htmlTransitionTableHTMLEntities));
echo "\n========================================================================\n";
echo $description;
echo "\n========================================================================\n";
print_r($listTableHTMLEntities);
echo "\n========================================================================\n";
// Convert all applicable characters to HTML entities
$htmlentitiesResult = htmlentities($listTableHTMLEntities, ENT_QUOTES, $charset);
echo $htmlentitiesResult;
echo "\n========================================================================\n";
// Decode it again
echo html_entity_decode($htmlentitiesResult, ENT_QUOTES, $charset);
echo "\n========================================================================\n";
// Convert special characters to HTML entities
// The translations performed are:
//
// ◦'&' (ampersand) becomes '&amp;'
// ◦'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
// ◦"'" (single quote) becomes '&#039;' (or &apos;
// ) only when ENT_QUOTES is set.
// ◦'<' (less than) becomes '&lt;'
// ◦'>' (greater than) becomes '&gt;'
$htmlspecialcharsResult = htmlspecialchars($listTableHTMLEntities, ENT_QUOTES, $charset);
echo $htmlspecialcharsResult;
echo "\n========================================================================\n";
// Decode it again
echo htmlspecialchars_decode($htmlspecialcharsResult, ENT_QUOTES);
echo "\n========================================================================\n";
}
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment