Re: [PHP4BETA] suggestion for new function str_change(string str,array transtable [,bool reverse])

From: Date: Sat, 20 Nov 1999 13:45:54 +0000
Subject: Re: [PHP4BETA] suggestion for new function str_change(string str,array transtable [,bool reverse])
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-6575@lists.php.net to get a copy of this message
On Sat, 20 Nov 1999, Rasmus Lerdorf wrote: > > lets start with this - i don't like the name > > > > function should search/replace a number of pairs in str and return the new > > string. > > > > sample: > > > > $trans = array("ü" => "ue","ß" => "ss"); > > // expand german umlauts > > > > $data = str_change("üß",$trans); // $data will be "uess"; > > > > // 3th argumet changed the "direction" in the transtable > > > > $data = str_change($data,$trans,true); // $data will be "üß" again. > > > > this function will make it easy to eg reverse an htmlspecialchars(). we > > could also make the table used by htmlspecialchars() and > > htmlentites() accessible in transtable format: > > > > $tt = get_htmltable(ISO-8859-1); // returns "EntTable" from html.c > > > > $plaindata = str_change($htmlencodeddata,$tt,1); // reverse htmlentities > > > > does that make sense? (any better ideas?) > > So it is like strtr() except for supporting multi-char > translations. Perhaps we can just overload the second argument to > strtr(). So if we have 2 args and the second arg is an array, assume the > array contains the translation table, otherwise the function takes 3 args > with the 2nd and 3rd being the standard single-char string representation > of the translation table. rasmus, i think extending strtr() is cooler that creating a new function, but - how do we pass the "do-the-reverse" flag to the function? i think we could instead create an array_switch_key_and_value() function (with a nicer name) so that: $trans = array("ue" => "ü", "oe" => "ö"); $str = "oeluebung"; $str = strtr($str,$trans); // now $str is "ölübung"; $trans = array_switch_key_and_value($trans); // now $trans = array("ü" => "ue", "ö" => "oe") $str = strtr($str,$trans); // now $str is "oeluebung" again. ? tc > > -Rasmus > > Thies C. Arntzen "One Big-Mac, Small Fries and a Coke!" Digital Collections Phone +49 40 235350 Fax +49 40 23535180 Hammerbrookstr. 93 20097 Hamburg / Germany

« previous php.version4 (#6575) next »