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?

4 Answers 4

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).

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

3 Comments

Ah, now that is a clever idea! Thank you :)
I like this method as well. If you ever change your variable names you wont have to modify your file also.
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!
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.

Comments

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?

Comments

0

You can use Eval with all the usual security caviats

Something like.

$string = getStringFromFile('sendingFrom');

$FilledIn = eval($string);

3 Comments

If only there was an eval_vars function.
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.
@idiqual, thats what the security Caveats are. However just don't use it is probably a better Caveat.

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.