2

I need to store data within a database, when I get the data from the database I need functions and variables in the string to be worked out as such.

Example

$str = "<p>Dear {$this->name},</p>"

I then store this in the database, and when I retrieve the string and run it through

eval("\$detail= \"$detail\";");

then the variable gets populated with the name. This is exactly what I needed and works fine.

The problem is I want to run a function with this variable as the parameter.

example. I would like to ucwords the variable.

I have tried:

$str = "<p>Dear {ucwords($this->name)},</p>"  //just echoed {ucword(->name)},
$str = "<p>Dear {ucwords($this->name)},</p>"  //Fatal error: Function name must be a string,

Am I going in the right direction? Is this at all possible?

2
  • @hsz eval() isn't inherently evil, but it's very easy to misuse/overuse. Commented Jan 9, 2013 at 15:04
  • first tim ei have used eval, i couldn't see any other way of having dynamic variables called from database? Commented Jan 9, 2013 at 15:12

3 Answers 3

3

You don't need to keep PHP code in database. This is a bad practice and also can lead to security vulnerabilities.

Instead store in database string like this:

<p>Dear [name],</p>

And when you retrieve it you can just do:

$stringFromDb = str_replace("[name]", $this->name, $stringFromDb);

or

$stringFromDb = str_replace("[name]", ucwords($this->name), $stringFromDb);

Other common approach is to use sprintf. So you need to store in database string with %s as placeholders for values.

Example:

<p>Dear %s,</p>

and replace with

$stringFromDb = sprintf($stringFromDb, ucwords($this->name));
Sign up to request clarification or add additional context in comments.

4 Comments

Hiya thanks for the reply. The problem is that i am will be creating a GUI for the database. This will allow the user to input any variable to be in the string. My model and related models have many variables
wow this site is so quick, thanks for the help. Ok, the issue is i wont know what variables the user have put into the string (email)
say for eg. The email could have $this->name, $this->balance, $this->street.
If you are building UI for your database then definitely you don't want PHP codes there, because it is a total mess. How you would explain to users what kind of variables you have in your code available for evaluation? I would suggest defining list of things that can be inserted into texts (like: [name], [email], [tel], etc) and build combo box with this values and offer convenient way for inserting them into text.
1

What you seem to be looking for is a simple templating language.

It's been a long while since I've written PHP (and I suddenly remember why...), but here's something I whipped up.

It should support both objects ($a->name) and arrays ($a["name"]) as input objects.

You can add new filters (name -> function name mapping) in $valid_filters.

$valid_filters = array("title" => "ucfirst", "upper" => "strtoupper");

function _apply_template_helper($match) {
  global $_apply_template_data, $valid_filters;
  $var = $match[1];
  $filter = $valid_filters[trim($match[2], ':')];
  $value = is_array($_apply_template_data) ? $_apply_template_data[$var] : $_apply_template_data->$var;
  if($filter && !empty($value)) $value = call_user_func($filter, $value);
  return !empty($value) ? $value : $match[0];
}

function apply_template($template, $data) {
  global $_apply_template_data;
  $_apply_template_data = $data;
  $result = preg_replace_callback('/\{\{(.+?)(:.+?)?\}\}/', "_apply_template_helper", $template);
  $_apply_template_data = null;
  return $result;
}

How to use it:

$template = "Hello {{name:title}}, you have been selected to win {{amount}}, {{salutation:upper}}";
echo apply_template($template, array("name"=>"john", "amount" => '$500,000', "salutation" => "congratulations"));

The result:

Hello John, you have been selected to win $500,000, CONGRATULATIONS

Comments

0

I have found the following works,

If i contain the function within the class itself then it can be called using the following code

<p>Dear {\$this->properCase(\$this->rl_account->name)},</p>

But i would like to be able to do this now without having the database have the code as Alex Amiryan mentions earlier.

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.