RE: [PHP] Re: template in message string
| From: | Dan Rossi | Date: | Thu, 17 Apr 2003 10:09:21 +0000 |
| Subject: | RE: [PHP] Re: template in message string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-144248@lists.php.net to get a copy of this message | ||
an example line would be nice ?? and basically my string will be returned
like
$string = "Something $var1 something else $var2";
from
$string = "Something {var1} something else {var2}";
that preg statement i posted seemed to work for me ??
so therefore
$var1 = "test";
$var2 = "test2";
echo $string; would hopefully return Something test something else test2
how can i do this without eval it didnt seem to working within a function :\
-----Original Message-----
From: SLanger@spirit21.de [mailto:SLanger@spirit21.de]
Sent: Thursday, April 17, 2003 5:15 PM
To: daniel@electroteque.org
Cc: php-general@lists.php.net
Subject: Re: [PHP] Re: template in message string
Hello
Look at the posting to "preg_replace brain teaser" for a faster way of
doing it.
Also you can use preg_replace with the /e modifier that way you don't have
to use eval! Even faster is preg_replace_callback see the earlier posting.
Also your regex is faulty '/{(.+?)}/'. The { and } should be escaped since
they are metacharacters. so it should read "/\\{(.+?)\\}/".
The double \\ will result in a \ in the final regex since PHP uses \ as a
metacharacter in strings.
Do you really want all characters to appear in the {}? This could lead to
unexpected results with your backreference used as a variable.
Example:
{$myvar} will result in $myvar being matched so what is evaluated later is
actually something like "Message $$myvar" which is definetly not what you
want.
Use something like \w+ to only allow wordcharacters or even better build
your own characterclass that actually contains all allowed characters such
as
[a-zA-Z0-9_-] This will only allow legal characters for php variables.
Regards
Stefan Langer