[prev in list] [next in list] [prev in thread] [next in thread] 

List:       nyphp-talk
Subject:    Re: [nyphp-talk] Eval a string
From:       justin <justin () justinhileman ! info>
Date:       2009-03-29 4:20:33
Message-ID: 1dc0e7e00903282120i3b24298eu2fc8188bdc6c030c () mail ! gmail ! com
[Download RAW message or body]

2009/3/28 Michele Waldman <mmwaldman@nyc.rr.com>:
> I have a database string that looks like "$variable word".
>
> When I assign it to a string the $variable is not being replaced by the
> actual value of $variable.
>
> Does anyone know how to force a replacement of a variable name with it's
> value in a string?


Evaling anything makes a bit uncomfortable, but most especially
strings pulled out of the database. I typically use something like
this instead:

// db string is '%variable% word'
$db_string = str_replace('%variable%', $variable, $db_string);


If you have lots of things to replace, strtr() works well:

$vars = array(
  '%foo%' => $foo,
  '%bar%' => $baz,
  '%baz%' => $baz
);
$db_string = strtr($db_string, $vars);

-- 
justin
http://justinhileman.com
_______________________________________________
New York PHP User Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/show_participation.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic