New function request - Variable parsing a string

php.internals

Herbert Groot Jebbink

22 years ago
Hi, To speed up a homebuild PHP framework I would like to have a new function that expands a string with the PHP variables in it, just like how variable parsing is done for a double-quotes string. string VariableParsingString ( string string ) Currently I do it with the next eval statement. eval('$result = "' . str_replace('"', '\\"', $string) . '";'); Greetings, Herbert

Rasmus Lerdorf

22 years ago
http://nl.php.net/parse_str On Tue, 31 Aug 2004, Herbert Groot Jebbink wrote:

Herbert Groot Jebbink

22 years ago
Rasmus Lerdorf wrote:
>> To speed up a homebuild PHP framework I would like to have a new >> function that expands a string with the PHP variables in it, just like >> how variable parsing is done for a double-quotes string.
> http://nl.php.net/parse_str
I don't want to parse variables FROM a string but I want to parse them INTO a string as described on the page: http://ie.php.net/manual/en/language.types.string.php#language.types.string.parsing Below a PHP example that shows the function I want. <?php $abc = '123'; $klm['klm'] = '456'; $xyz->xyz = '789'; $string = '* {$abc} * {$klm[\'klm\']} * {$xyz->xyz} *'; $result = parseVarsIntoString ($string); echo $result; function parseVarsIntoString ($string) { extract($GLOBALS); eval('$result = "' . str_replace('"', '\\"', $string) . '";'); return $result; } ?> Greetings, Herbert

Philip Olson

22 years ago
> I don't want to parse variables FROM a string but I want to parse them > INTO a string as described on the page: > > http://ie.php.net/manual/en/language.types.string.php#language.types.string.parsing > > Below a PHP example that shows the function I want. > > <?php > > $abc = '123'; > $klm['klm'] = '456'; > $xyz->xyz = '789'; > > $string = '* {$abc} * {$klm[\'klm\']} * {$xyz->xyz} *'; > > $result = parseVarsIntoString ($string); > > echo $result; > > function parseVarsIntoString ($string) { > > extract($GLOBALS); > > eval('$result = "' . str_replace('"', '\\"', $string) . '";'); > > return $result; > > }
Just use double quotes, no need for a function: $string = "* {$abc} * {$klm['klm']} * {$xyz->xyz} *"; echo $string; Consider rereading that manual page. Regards, Philip

Sara Golemon

22 years ago
> Just use double quotes, no need for a function: > > $string = "* {$abc} * {$klm['klm']} * {$xyz->xyz} *"; > echo $string; >
He wants to store an UNinterpolated string somewhere (like a DB or text file), then interpolate it at run-time. $sitename = 'php.net'; $username = 'pollita'; $string = $db->getOne('SELECT somestring FROM thetable WHERE id=\'foo\''); /* $string now contains something like: 'Welcome to {$sitename}. You are logged in as {$username}' * He wants $string to get interpolated into: 'Welcome to php.net. You are logged in as pollita' */ I've thought this would be a nice thing to have as well, but it's not something that can be trivially ripped out of the compiler and I havn't personally had the motivation to actually get it done. -Sara

Herbert Groot Jebbink

22 years ago
Sara Golemon wrote:
>> $string = "* {$abc} * {$klm['klm']} * {$xyz->xyz} *"; >> echo $string; >> > He wants to store an UNinterpolated string somewhere (like a DB or text > file), then interpolate it at run-time.
indeed, and it's usage is in a driver that calls include files for the business logic and them merge the variables set by this include file with template files. This driver has no knowlegde which varibles are used so the printf solution I saw in this thread will not work.
> I've thought this would be a nice thing to have as well, but it's not > something that can be trivially ripped out of the compiler and I havn't > personally had the motivation to actually get it done.
do you have a whishlist with books or cd's online at Amazon or so, maybe I can give you that way some motivation :-) Greetings, Herbert

Antony Dovgal

22 years ago
On Wed, 1 Sep 2004 08:22:14 -0700 "Sara Golemon" <pollita@php.net> wrote:
> > Just use double quotes, no need for a function: > > > > $string = "* {$abc} * {$klm['klm']} * {$xyz->xyz} *"; > > echo $string; > > > He wants to store an UNinterpolated string somewhere (like a DB or > text file), then interpolate it at run-time. > > $sitename = 'php.net'; > $username = 'pollita'; > $string = $db->getOne('SELECT somestring FROM thetable WHERE > id=\'foo\'');/* $string now contains something like: 'Welcome to > {$sitename}. You are logged in as {$username}' > * He wants $string to get interpolated into: 'Welcome to php.net. > You are > logged in as pollita' > */ > > I've thought this would be a nice thing to have as well, but it's not > something that can be trivially ripped out of the compiler and I > havn't personally had the motivation to actually get it done.
I'd propose to the author to look at some template engine (I'm sure almost all of them already have this functionality implemented).
-- Wbr, Antony Dovgal aka tony2001 tony2001@phpclub.net || antony@dovgal.com

Daniel C.

22 years ago
While this would definitely be handy, it seems (to me, and I am not by any means an expert on these things,) that it would make more sense to add this to a string-munging library instead of integrating it into the PHP language itself. Dan On Wed, 1 Sep 2004 08:22:14 -0700, Sara Golemon <pollita@php.net> wrote:

Wico de Leeuw

22 years ago
Hiya, Could somebody please take a look at it, it's has been "not implented" for more then a year now, and i really could use it. (i tried to modify/fix it myself, but i don't know much c and only got degmentation faults :) Without this function you can get multiple urls at the same time but you have to wait until they all are retrieved, which can take a long time if one times out) Gr, Wico