strtr(),get_html_translation_table(),array_flip() - or "how to reverse htmlspecialchars()/htmlenities()"

From: Date: Sun, 21 Nov 1999 13:42:24 +0000
Subject: strtr(),get_html_translation_table(),array_flip() - or "how to reverse htmlspecialchars()/htmlenities()"
Groups: php.dev php.version4 
Request: Send a blank email to php-dev+get-12853@lists.php.net to get a copy of this message
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

« previous php.dev (#12853) next »