0

I've had a good search and not found anything relevant to my issue, so please dont down vote without showing me where you have a relevant example...

Trying to build template files as a text document to simplify it for others to modify later, but I can't work out how to get PHP to recognize the variables in it.

What I want to work:

//template.txt
Every time I find $foo,
I get a little $bar,
template text.

note: The line breaks are critical for position.

//handler.php
$foo = 'Beer';
$bar = 'Excited';
$file = file('template.txt');

foreach ($file as $line) {
    echo $line;
}

What I want this to return:

Every time I find Beer,
I get a little Excited,
template text.

but it is escaping the variable mark ($), and is returning:

Every time i find $foo,
I get a little $bar,
template text.

I tried to do a str replace with:

if (strpos($line, '\$foo')) {
    str_replace('\$foo', $foo, $line);
}

(tried with and without the $ symbol, and many variations on this) at best I can get it to recognise the variable name but it is not replacing.

Has anyone had any experience with this?

Really not finding much info on this online. Any help will be greatly appreciated.

Craig B
  • 499
  • 4
  • 13

1 Answers1

1

When you're using single quote it's auto escaping everything in there, so you're telling it to replace \$foo not $foo

str_replace('$foo', $foo, $line);

Also you don't really need the if statement, if it can't find it it's not going to do anything anyway.

If you're going to have more variables you could set up a pair of arrays and instead of iterating through it line by line just throw the whole string at it at once

$file = file('template.txt');
$search = array('$foo', '$bar');
$replace = array('Beer', 'Excited');
$file = str_replace($search, $replace, $file);
foreach ($file as $i) {
    echo $i;
}
Brett Harris
  • 347
  • 1
  • 7
  • Yea i managed to get it to do the one, still have to through the name in there, painful af haha. Am I better to encapsulate the variable in something else? ie. "[ ]" ? . Is it possible to have it pluck the name of the variable? ie. have a substr starting at the $ and ending at the white space (or special char) and replace that with a $(substr) ? – Craig B Mar 22 '17 at 00:16
  • 1
    Well, you could just run with numbers, and build only the array of replacements, so like $1 you'd pull out the 1 with regex or str_pos and substr and $replace = array('bl', 'ah', 'af'); str_replace('$1', $replace[1], $file); – Brett Harris Mar 22 '17 at 00:23
  • Sorry, second question, with the arrays in str replace, if I have many variables that across several files, if the variable isnt found will it throw out the replace values? ie. `$replace = array($drink, $food, $drugs, $sports);` if it hits a file where $drugs is not relevant will it place the drug variable where it should be sports? – Craig B Mar 22 '17 at 00:25
  • It'll go as deep into the replacement array as it can, if your search array is 3 and replace is 5 it'll never do anything with the last 2 in replace. If the search array is longer than the replace array it'll just empty string. – Brett Harris Mar 22 '17 at 00:27
  • Think of it like for ($i=0; $i < count($search) ; $i++) { $file = str_replace($seach[$i], $replace[$i], $file); } – Brett Harris Mar 22 '17 at 00:32
  • 1
    It's better if you use the function strtr check this link http://stackoverflow.com/questions/15065387/how-replace-variable-in-string-with-value-in-php – AgeValed Mar 22 '17 at 00:32
  • Have tested both, and both work fantastically, however @AgeValed's looks like it will be a bit simpler and reduce the file size alot. Thank you both :) – Craig B Mar 22 '17 at 00:48