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

From: Date: Sat, 20 Nov 1999 12:00:11 +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-6871@lists.php.net to get a copy of this message
I think that the "general" substitution case is much more difficult to build efficiently. This problem is nearly the same as compiling a lex scanner which recognizes multiple patterns within an input string. So, the idea of making a simple "boolean" flag to make the reverse translation is a bad idea in the given example, unless we do a massive programming effort to support it. What I think is that such a function should be able to use a more generic run-time lex scanner, which takes an input stream and produces lexical actions performing data substitution on the output. The main problem is that the classic "lex" implementation is not built on making run-time recognizers, that can be compiled on the fly, and then used to make this kind of traversal. But may be there may exist a work on the net that makes "lex" work on the fly, compiling a lex definition (here the substitution array), with or without support of regular expressions, into a lexical traversal tree, that would be built dynamically, stored as an object, and that can be further reused multiple times to make such changes. In the proposed function, I think that there is a big performance impact on allowing such a function to make such substitutions by scanning all candidate strings to make the substitution. There's an improvement however by first building a compiled definition of the traversal tree, but this compilation also has an impact on performance. So the best thing would be to create not only ONE function, but two, like this: $translator = lex_compile($tran); $data = lex_exec($source, $translator); having this done, the translator could be compiled somewhere in the server and stored as a common reusable resource. There could be a error-handling procedure, to test whever the given $tran substitution array is not ambiguous, and there is still the need to specify the behavior of lexical entities that start with the same symbols (for example "ue" and "u"). There is finally the need to specify the default substitution string (i.e., most of the time the default action would be to translate "u" as "u", without having to specify all source entities). I think that there are two possible actions: either copy unspecified source entities to the output, either delete them (lex_compile could allow an optional flag to manage it). So why not this kind of definition: $translator = lex_compile($tran, $flags); with $flags having the behaviour of flags used in PCRE. May be it could be nice to have a flag to disable special regexp metacharacters, plus a flag to specify the behaviour of default substitution when regexp are not used (copy or delete or error ?). When regexp are used, the behavior could be specified as one of the substitution elements, like: (".", "&") which does the copy, (".", "") which does the deletion, and no (".", ...) rules which would produce an error exception when the input stream does not match one of the source strings in the $trans array. Once this lexical scanner builder function is defined, building the proposed function would be very easy to implement. However I would propose the following kind of function: $tran1 = array("ue", "ss"); $tran2 = array("ü", "ß"); ... $data1 = str_lex($source1, $tran1, $tran2, $flags); $data2 = str_lex($source2, $tran1, $tran2, $flags); with $flags designed for the lex_compile function. But what if multiple occurence of such calls has to be done ? I don't think that compiling/executing/deleting the scanner at each call woul be a good idea. So I would prefer to have something like: $tran1 = array("ue", "ss"); $tran2 = array("ü", "ß"); $scanner = lex($source, $tran1, $tran2, $flags); ... $data1 = str_lex($source1, $scanner); $data2 = str_lex($source2, $scanner); which uses a precompiled scanner, built only once, and then fastly reused for further string substitutions... What do you think ? ----- Original Message ----- From: Andi Gutmans <andi@zend.com> To: Thies C. Arntzen <thies@digicol.de>; <php4beta@lists.php.net> Sent: Saturday, November 20, 1999 12:16 PM Subject: Re: [PHP4BETA] suggestion for new function str_change(string str,array transtable [,bool reverse]) Do you want it to be able to substitute only one character strings or is it supposed to be general? For example taking your example do you want to allow: $trans = array("ue" => "ü","ss" => "ß"); // expand german umlauts Andi At 11:25 AM 11/20/99 +0100, Thies C. Arntzen 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?) > >tc > > > > >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 > > >-- >PHP 4.0 Beta Mailing List <http://www.php.net/version4/> >To unsubscribe, e-mail: php4beta-unsubscribe@lists.php.net >For additional commands, e-mail: php4beta-help@lists.php.net >To contact the list administrators, e-mail: php4beta-admin@lists.php.net --- Andi Gutmans <andi@zend.com> http://www.zend.com/ -- PHP 4.0 Beta Mailing List <http://www.php.net/version4/> To unsubscribe, e-mail: php4beta-unsubscribe@lists.php.net For additional commands, e-mail: php4beta-help@lists.php.net To contact the list administrators, e-mail: php4beta-admin@lists.php.net

« previous php.version4 (#6871) next »