1

For reasons I'd rather not get into right now, I have a string like so:

<div>$title</div>

that gets stored in a database using mysql_real_escape_string.

During normal script execution, that string gets parsed and stored in a variable $string and then gets sent to a function($string).

In this function, I am trying to:

function test($string){
  $title = 'please print';
  echo $string;
}
//I want the outcome to be <div>please print</div>

This seems like the silliest thing, but for the life of me, I cannot get it to "interpret" the variables.

I've also tried,

echo html_entity_decode($string);
echo bin2hex(html_entity_decode($string)); //Just to see what php was actually seeing I thought maybe the $ had a slash on it or something.

I decided to post on here when my mind kept drifting to using EVAL().

This is just pseudocode, of course. What is the best way to approach this?

4 Answers 4

3

Your example is a bit abstract. But it seems like you could do pretty much what the template engines do for these case:

function test($string){
   $title = 'please print';

   $vars = get_defined_vars();
   $string = preg_replace('/[$](\w{3,20})/e', '$vars["$1"]', $string);

   echo $string;
}

Now actually, /e is pretty much the same as using eval. But at least this only replaces actual variable names. Could be made a bit more sophisticated still.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think there is a way to get that to work. You are trying something like this:

$var = "cute text";
echo 'this is $var';

The single quotes are preventing the interpreter from looking for variables in the string. And it is the same, when you echo a string variable.

The solution will be a simple str_replace.

echo str_replace('$title', $title, $string);

But in this case I really suggest Template variables that are unique in your text.

4 Comments

Smarty uses curly braces around a PHP style var, {$like} {$these}.
Just to note if you change your single apostrophe to a double apostrophe your first example will work, this also goes for setting the string variable, so its not necessarily when you echo a string variable. If you set $string = 'this is $var'; you will get exactly that on echo, but use $string = "this is $var"; you will get this is cute text when echoing $string.
@Scoobler You are right. The snippet I posted was just intended to demonstrate what happens in the questions example.
You're welcome! Please note that @markus's answer is more common for single replacements. My method is mainly used for templates where several variables exist in one template string.
1

You just don't do that, a variable is a living thing, it's against its nature to store it like that, flat and dead in a string in the database.

If you want to replace some parts of a string with the content of a variable, use sprintf().

Example

$stringFromTheDb = '<div>%s is not %s</div>';

Then use it with:

$finalString = sprintf($stringFromTheDb, 'this', 'that');

echo $finalString;

will result in:

<div>this is not that</div>

1 Comment

How could i forget about this? Well both answers (mine and this one) have use cases but in general I prefer this. +1
1

If you know that the variable inside the div is $title, you can str_replace it.

function test($string){
  $title = 'please print';
  echo str_replace('$title', $title, $string);
}

If you don't know the variables in the string, you can use a regex to get them (I used the regex from the PHP manual).

function test($string){
  $title = 'please print';
  $vars = '/(?<=\$)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/';
  preg_match_all($vars, $string, $replace);
  foreach($replace[0] as $r){
    $string = str_replace('$'.$r, $$r, $string);
  }
  echo $string;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.