|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
get_html_translation_tableDescriptionarray get_html_translation_table ( [int table [, int quote_style]] )
get_html_translation_table() will return the
translation table that is used internally for
htmlspecialchars() and
htmlentities().
There are two new constants
(HTML_ENTITIES,
HTML_SPECIALCHARS) that allow you to
specify the table you want. Default value for table
is HTML_SPECIALCHARS. And as in the
htmlspecialchars() and
htmlentities() functions you can optionally
specify the quote_style you are working with. The default is
ENT_COMPAT mode. See the description of these modes in
htmlspecialchars().
Przykład 1. Translation Table Example |
<?php
$trans = get_html_translation_table(HTML_ENTITIES);
$str = "Hallo & <Frau> & Krämer";
$encoded = strtr($str, $trans);
?>
|
|
The $encoded variable will now contain:
"Hallo & <Frau> &
Krämer".
Another interesting use of this function is to, with help of array_flip(),
change the direction of the translation.
The content of $original would be: "Hallo &
<Frau> & Krämer".
See also htmlspecialchars(),
htmlentities(),
strtr(), and
array_flip().
User Contributed NotesPatrick nospam at nospam mesopia dot com
30-May-2005 04:00
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 ' while
<?php
$translatedString = htmlentities($string,ENT_QUOTES);
?>
returns it as being equal to '
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
Alex Minkoff
19-May-2005 01:30
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. ;)
ryan at ryancannon dot com
26-Jan-2005 11:05
In XML, you can't assume that the doctype will include the same character entity definitions as HTML. XML authors may require character references instead. The following two functions use get_html_translation_table() to encode data in numeric references. The second, optional argument can be used to substitute a different translation table.
function xmlcharacters($string, $trans='') {
$trans=(is_array($trans))? $trans:get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";
return strtr($string, $trans);
}
function xml_character_decode($string, $trans='') {
$trans=(is_array($trans))? $trans:get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";
$trans=array_flip($trans);
return strtr($string, $trans);
}
kevin_bro at hostedstuff dot com
03-Jan-2003 03:06
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);
}
alan at akbkhome dot com
04-Jun-2002 07:00
If you want to decode all those { 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);
}
programmer at bardware dot de
03-Aug-2001 02:28
It doesn't work if text is CP850 encoded. I obtain text CP850 encoded from a database since it was imported in the DB from a CSV file that was created in DOS text format.
dirk at hartmann dot net
19-Jun-2001 10:41
get_html_translation_table
It works only with the first 256 Codepositions.
For Higher Positions, for Example ф
(a kyrillic Letter) it shows the same.
jon+php-dev at unequivocal dot co dot uk
10-Sep-2000 04:38
Caution: This function is implemented separately from htmlentities() and htmlspecialchars() in the PHP source, and hence is quite likely to diverge from these functions.
In particular, in 4.0.2 htmlspecialchars() escapes the single quote character, whereas the translation table returned by this function does not.
|