PHP 8.3.4 Released!

get_html_translation_table

(PHP 4, PHP 5, PHP 7, PHP 8)

get_html_translation_tablehtmlspecialchars() ve htmlentities() tarafından kullanılan dönüşüm tablosunu döndürür

Açıklama

get_html_translation_table(int $tablo = HTML_SPECIALCHARS, int $seçenekler = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, string $kodlama = "UTF-8"): array

get_html_translation_table() işlevi, htmlspecialchars() ve htmlentities() tarafından kullanılan dahili dönüşüm tablosunu döndürür.

Bilginize:

Özel karakterler çeşitli yollarla kodlanabilir. Örneğin " karakteri ", " veya &#x22 olarak kodlanabilir. get_html_translation_table() işlevi sadece htmlspecialchars() ve htmlentities() tarafından kullanılan biçimi döndürür.

Bağımsız Değişkenler

tablo

Hangi tabloyu istediğinizi HTML_ENTITIES ve HTML_SPECIALCHARS sabitlerinden biri ile belirtebilirsiniz. HTML_SPECIALCHARS öntanımlıdır.

seçenekler

Tablonun hangi tırnak işaretlerini içereceğini ve tablonun hangi belge türü için olduğunu belirten aşağıdaki seçeneklerin bir veya daha fazlasının bit maskesi. ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 öntanımlıdır.

Kullanılabilen seçeneklerin sabitleri
Sabit Adı Açıklama
ENT_COMPAT Tablo çift tırnaklı öğeleri içerir, tek tırnaklıları değil.
ENT_QUOTES Tablo hem çift tırnaklı hem de tek tırnaklı öğeleri içerir.
ENT_NOQUOTES Tablo ne çift tırnaklı ne de tek tırnaklı öğeleri içerir.
ENT_SUBSTITUTE Geçersiz kod dizilimi için boş bir dizge döndürülmeyip dizilimin yerine Unicode Değiştirme Karakteri (U+FFFD veya &#FFFD;) yerleştirilir.
ENT_HTML401 Tablo HTML 4.01 içindir.
ENT_XML1 Tablo XML 1 içindir.
ENT_XHTML Tablo XHTML içindir.
ENT_HTML5 Tablo HTML 5 içindir.

kodlama

Kullanılacak kodlama. Belirtilmezse, UTF-8 öntanımlıdır.

Aşağıdaki karakter kümeleri desteklenmektedir.

Desteklenen karakter kümeleri
Karakter kümesi Takma adı Açıklama
ISO-8859-1 ISO8859-1 Batı Avrupa, Latin-1
ISO-8859-5 ISO8859-5 Az kullanılan kril küme (Latin/Cyrillic).
ISO-8859-15 ISO8859-15 Doğu Avrupa, Latin-9. Euro işaretini ekler, Fransızca ve Fince harfler Latin-1'de (ISO-8859-1) yoktur.
UTF-8   ASCII uyumlu 8 bitlik çok baytlı Unicode.
cp866 ibm866, 866 DOS'a özgü Kril karakter kümesi.
cp1251 Windows-1251, win-1251, 1251 Windows'a özgü Kril karakter kümesi.
cp1252 Windows-1252, 1252 Doğu Avrupa için Windows'a özgü karakter kümesi.
KOI8-R koi8-ru, koi8r Rusça.
BIG5 950 Geleneksel Çince, aslında Tayvan'da kullanılır.
GB2312 936 Basitleştirilmiş Çince, ulusal standart karakter kümesi.
BIG5-HKSCS   Geleneksel Çince, Hong Kong eklentisi ile Big5.
Shift_JIS SJIS, 932 Japonca.
EUC-JP EUCJP, eucJP-win Japonca.
MacRoman   Mac OS tarafından kullanılmış karakter kümesi.
''   Boş bir dize, karakter kümesini, komut dosyası kodlamasından (Zend multibyte), default_charset ve geçerli yerel ayardan (bkz. nl_langinfo() ve setlocale()) algılamayı bu sırayla etkinleştirir. Önerilmez.

Bilginize: Bunlardan başka karakter kümesi tanınmaz. Yerine öntanımlı karakter kümesi kullanılır ve bir uyarı çıktılanır.

Dönen Değerler

Dönüşüm tablosunu bir dizi olarak döndürür.

Sürüm Bilgisi

Sürüm: Açıklama
8.1.0 seçenekler bağımsız değişkeninin öntanımlı değeri ENT_COMPAT iken ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 oldu.

Örnekler

Örnek 1 - Dönüşüm tablosu örneği

<?php
var_dump
(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_HTML5));
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

array(1510) {
  ["
"]=>
  string(9) "&NewLine;"
  ["!"]=>
  string(6) "&excl;"
  ["""]=>
  string(6) "&quot;"
  ["#"]=>
  string(5) "&num;"
  ["$"]=>
  string(8) "&dollar;"
  ["%"]=>
  string(8) "&percnt;"
  ["&"]=>
  string(5) "&amp;"
  ["'"]=>
  string(6) "&apos;"
  // ...
}

Ayrıca Bakınız

add a note

User Contributed Notes 13 notes

up
12
kevin at cwsmailbox dot xom
13 years ago
Be careful using get_html_translation_table() in a loop, as it's very slow.
up
13
michael dot genesis at gmail dot com
12 years ago
The fact that MS-word and some other sources use CP-1252, and that it is so close to Latin1 ('ISO-8859-1') causes a lot of confusion. What confused me the most was finding that mySQL uses CP-1252 by default.

You may run into trouble if you find yourself tempted to do something like this:
<?php
$trans
[chr(149)] = '&bull;'; // Bullet
$trans[chr(150)] = '&ndash;'; // En Dash
$trans[chr(151)] = '&mdash;'; // Em Dash
$trans[chr(152)] = '&tilde;'; // Small Tilde
$trans[chr(153)] = '&trade;'; // Trade Mark Sign
?>

Don't do it. DON'T DO IT!

You can use:
<?php
$translationTable
= get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'WINDOWS-1252');
?>

or just convert directly:
<?php
$output
= htmlentities($input, ENT_NOQUOTES, 'WINDOWS-1252');
?>

But your web page is probably encoded UTF-8, and you probably don't really want CP-1252 text flying around, so fix the character encoding first:
<?php
$output
= mb_convert_encoding($input, 'UTF-8', 'WINDOWS-1252');
$ouput = htmlentities($output);
?>
up
2
Kenneth Kin Lum
15 years ago
to display the mapping on a webpage no matter what the server encoding is, this can be used

echo "<pre>\n";
echo htmlentities(print_r((get_html_translation_table(HTML_SPECIALCHARS)), true));
echo htmlentities(print_r((get_html_translation_table(HTML_ENTITIES)), true));

since get_html_translation_table() actually gives the special chars in iso-8859-1 (Latin-1) encoding, so to see the tables correctly using

print_r(get_html_translation_table(HTML_ENTITIES));

your server needs to give a HTTP header as iso-8859-1, unless you use header() or manually set the browser's encoding setting to iso-8859-1. And you need to view the source of the page to see the mapping. (except English version of IE 7 outputs the page source as iso-8859-1 anyway).
up
2
dirk at hartmann dot net
22 years ago
get_html_translation_table
It works only with the first 256 Codepositions.
For Higher Positions, for Example &#1092;
(a kyrillic Letter) it shows the same.
up
0
iain (duh) workingsoftware.com.au
16 years ago
I wrote a quick little function for converting something like '&middot;' into '&#183;':

$to_convert = '&middot;';
$table = get_html_translation_table(HTML_ENTITIES);
$equiv = '&#'.ord(array_search($to_convert,$table)).';';
up
0
Jérôme Jaglale
17 years ago
htmlentities includes htmlspecialchars, so here's how to convert an UTF-8 string :
htmlentities($string, ENT_QUOTES, 'UTF-8');
up
0
Patrick nospam at nospam mesopia dot com
18 years ago
Not sure what's going on here but I've run into a problem that others might face as well...

<?php

$translations
= array_flip(get_html_translation_table(HTML_ENTITIES,ENT_QUOTES));

?>

returns the single quote ' as being equal to &#39; while

<?php

$translatedString
= htmlentities($string,ENT_QUOTES);

?>
returns it as being equal to &#039;

I've had to do a specific string replacement for the time being... Not sure if it's an issue with the function or the array manipulation.

-Pat
up
-2
Maurizio Siliani at trident dot it
16 years ago
If you have troubles (like me) getting data from ISO-8859-1 encoded forms where user copy and paste from word, this routine could be useful.
It adds to the standard get_html_translation_table the codes of the characters usually M$ Word replacs into typed text.
Otherwise those characters would never be displayed correctly in html output.

function get_html_translation_table_CP1252() {
$trans = get_html_translation_table(HTML_ENTITIES);
$trans[chr(130)] = '&sbquo;'; // Single Low-9 Quotation Mark
$trans[chr(131)] = '&fnof;'; // Latin Small Letter F With Hook
$trans[chr(132)] = '&bdquo;'; // Double Low-9 Quotation Mark
$trans[chr(133)] = '&hellip;'; // Horizontal Ellipsis
$trans[chr(134)] = '&dagger;'; // Dagger
$trans[chr(135)] = '&Dagger;'; // Double Dagger
$trans[chr(136)] = '&circ;'; // Modifier Letter Circumflex Accent
$trans[chr(137)] = '&permil;'; // Per Mille Sign
$trans[chr(138)] = '&Scaron;'; // Latin Capital Letter S With Caron
$trans[chr(139)] = '&lsaquo;'; // Single Left-Pointing Angle Quotation Mark
$trans[chr(140)] = '&OElig; '; // Latin Capital Ligature OE
$trans[chr(145)] = '&lsquo;'; // Left Single Quotation Mark
$trans[chr(146)] = '&rsquo;'; // Right Single Quotation Mark
$trans[chr(147)] = '&ldquo;'; // Left Double Quotation Mark
$trans[chr(148)] = '&rdquo;'; // Right Double Quotation Mark
$trans[chr(149)] = '&bull;'; // Bullet
$trans[chr(150)] = '&ndash;'; // En Dash
$trans[chr(151)] = '&mdash;'; // Em Dash
$trans[chr(152)] = '&tilde;'; // Small Tilde
$trans[chr(153)] = '&trade;'; // Trade Mark Sign
$trans[chr(154)] = '&scaron;'; // Latin Small Letter S With Caron
$trans[chr(155)] = '&rsaquo;'; // Single Right-Pointing Angle Quotation Mark
$trans[chr(156)] = '&oelig;'; // Latin Small Ligature OE
$trans[chr(159)] = '&Yuml;'; // Latin Capital Letter Y With Diaeresis
ksort($trans);
return $trans;
}
up
-3
Alex Minkoff
18 years ago
If you want to display special HTML entities in a web browser, you can use the following code:

<?
$entities = get_html_translation_table(HTML_ENTITIES);
foreach ($entities as $entity) {
$new_entities[$entity] = htmlspecialchars($entity);
}
echo "<pre>";
print_r($new_entities);
echo "</pre>";
?>

If you don't, the key name of each element will appear to be the same as the element content itself, making it look mighty stupid. ;)
up
-3
kumar at chicagomodular.com
21 years ago
without heavy scientific analysis, this seems to work as a quick fix to making text originating from a Microsoft Word document display as HTML:

<?php
function DoHTMLEntities ($string)
{
$trans_tbl = get_html_translation_table (HTML_ENTITIES);

// MS Word strangeness..
// smart single/ double quotes:
$trans_tbl[chr(145)] = '\'';
$trans_tbl[chr(146)] = '\'';
$trans_tbl[chr(147)] = '&quot;';
$trans_tbl[chr(148)] = '&quot;';

// Acute 'e'
$trans_tbl[chr(142)] = '&eacute;';

return
strtr ($string, $trans_tbl);
}
?>
up
-6
robertn972 at gmail dot com
15 years ago
I found this useful in converting latin characters

<?php
function convertLatin1ToHtml($str) {
$allEntities = get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES);
$specialEntities = get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES);
$noTags = array_diff($allEntities, $specialEntities);
$str = strtr($str, $noTags);
return
$str;
}
?>
up
-5
kevin_bro at hostedstuff dot com
21 years ago
Alans version didn't seem to work right. If you're having the same problem consider using this slightly modified version instead:

function unhtmlentities ($string) {
$trans_tbl = get_html_translation_table (HTML_ENTITIES);
$trans_tbl = array_flip ($trans_tbl);
$ret = strtr ($string, $trans_tbl);
return preg_replace('/&#(\d+);/me',
"chr('\\1')",$ret);
}
up
-8
alan at akbkhome dot com
21 years ago
If you want to decode all those &#123; symbols as well....

function unhtmlentities ($string) {
$trans_tbl = get_html_translation_table (HTML_ENTITIES);
$trans_tbl = array_flip ($trans_tbl);
$ret = strtr ($string, $trans_tbl);
return preg_replace('/\&\#([0-9]+)\;/me',
"chr('\\1')",$ret);
}
To Top