[PHP-DEV] strtr(),get_html_translation_table(),array_flip() - or "how to reverse htmlspecialchars()/htmlenities()" From: Thies C. Arntzen (thies <email protected>)
Date: 11/21/99

hi,
just committed some useful stuff to PHP 4 cvs: (maybe some of the doc-guys
feels strong enmoght to write the docs for it?)

strtr() can now be called with only 2 arguments. if called with just 2
args it behaves in a new way: arg2 then has to be an array that contains
string->string pairs that will be replaces in the source-string. strtr
will always look for the longest possible match 1st and will *NOT* try to
replace in stuff that it has already worked on!

$trans = array("hallo" => "hi", "hi" => "hallo");
echo strtr("hi all, i said hallo",$trans)."\n";

will show:
hallo all, i said hi

get_html_translation_table(int which) will return the translation tables
that php used internally for htmlspecialchars() and htmlentities(). there
are two new defines (HTML_ENTITIES, HTML_SPECIALCHARS) that allow you to
specify the table you want (this leaves us room for implemeting other that
8859-1 charsets;-)

so you can now write your own htmlentities() that works *exactly* like
the builtin one:

$trans = get_html_translation_table(HTML_ENTITIES);
$str = "Hallo & <Pallo> & küßchen";
$encoded = strtr($str,$trans);

$encoded will now be:
Hallo &amp; &lt;Pallo&gt; &amp; k&uuml;&szlig;chen

the cool "new" this is that using array_flip() you can change the
direction of the translation!

$trans = array_flip($trans);
$original = strtr($str,$trans);

and $original will be
Hallo & <Pallo> & küßchen

again!

have fun,
tc

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>