0

I'm storing some strings within a *.properties file. An example of a string is:

sendingFrom=Sending emails from {$oEmails->agentName}, to {$oEmails->customerCount} people.

My function takes the value from sendingFrom and then outputs that string on the page, however it doesn't automatically parse the {$oEmails->agentName} within. Is there a way, without manually parsing that, for me to get PHP to convert the variable from a string, into what it should be?

James
  • 5,137
  • 5
  • 40
  • 80

4 Answers4

4

If you can modify your *.properties, here is a simple solution:

# in file.properties
sendingFrom = Sending emails from %s, to %s people.

And then replacing %s with the correct values, using sprintf:

// Get the sendingFrom value from file.properties to $sending_from, and:
$full_string = sprintf($sending_from, $oEmails->agentName, $oEmails->customerCount);

It allows you to separate the logic of your app (the variables, and how you get them) from your presentation (the actual string scheme, stored in file.properties).

ldiqual
  • 15,015
  • 6
  • 52
  • 90
  • Ah, now that is a clever idea! Thank you :) – James May 23 '12 at 17:00
  • 1
    I like this method as well. If you ever change your variable names you wont have to modify your file also. – EmmanuelG May 23 '12 at 17:03
  • That's perfect. I have a function which outputs the message with some extra formatting. I've just used `vsprintf` to take my arguments (from 3 onwards) and output. Clean and simple. Awesome idea, thank you very much! – James May 23 '12 at 17:09
2

Just an alternative.

$oEmails = new Emails('Me',4);
$str = 'sendingFrom=Sending emails from {$oEmails->agentName}, to {$oEmails->customerCount} people.';

// --------------

$arr = preg_split('~(\{.+?\})~',$str,-1,PREG_SPLIT_DELIM_CAPTURE);
for ($i = 1; $i < count($arr); $i+=2) {
    $arr[$i] = eval('return '.substr($arr[$i],1,-1).';');
}
$str = implode('',$arr);
echo $str;
// sendingFrom=Sending emails from Me, to 4 people.
inhan
  • 7,394
  • 2
  • 24
  • 35
1

as others mentioned eval wouldn't be appropriate, I suggest a preg_replace or a preg_replace_callback if you need more flexibility.

preg_replace_callback('/\$(.+)/', function($m) {
     // initialise the data variable from your object  
     return $data[$m[1]];
}, $subject);

Check this link out as well, it suggests the use of strstr How replace variable in string with value in php?

Community
  • 1
  • 1
filype
  • 8,034
  • 10
  • 40
  • 66
0

You can use Eval with all the usual security caviats

Something like.

$string = getStringFromFile('sendingFrom');

$FilledIn = eval($string);
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
  • 2
    If only there was an `eval_vars` function. – James May 23 '12 at 17:00
  • 2
    There are plenty of security issues related to `eval`. For instance, if someone manages to modify `file.properties` he can do very bad things as php will run the code. Don't use `eval`. – ldiqual May 23 '12 at 17:03
  • @idiqual, thats what the security Caveats are. However just don't use it is probably a better Caveat. – Toby Allen May 23 '12 at 17:05