0

I am working on a script with templates. So I have this PHP code:

<?php
$string = "TEST";
echo(file_get_contents('themes/default/test.html'));
?>

And I have this HTML (the test.html file):

<html>
  <p>{$string}</p>
</html>

How can I make PHP actually display the variable inside the curly brackets? At the moment it displays {$string}.

P.S: The string might also be an object with many many variables, and I will display them like that: {$object->variable}.

P.S 2: The HTML must stay as it is. This works:

$string = "I'm working!"
echo("The string is {$string}");

I need to use the same principle to display the value.

Sergiu N
  • 9
  • 1
  • 4
  • 1
    Possible duplicate of [PHP replace string after using file\_get\_contents](http://stackoverflow.com/questions/14466090/php-replace-string-after-using-file-get-contents) – mplungjan Jan 18 '16 at 10:48
  • 1
    Checkout this Question on SO: http://stackoverflow.com/questions/15065387/how-replace-variable-in-string-with-value-in-php – Saqib Amin Jan 18 '16 at 10:48
  • e.g. `echo str_replace('{$string}', $string, file_get_contents('themes/default/test.html'));` – mplungjan Jan 18 '16 at 10:50
  • Thank you, guys, for the links. The thing is the string might be retrieved from an object with many many variables, so I need to use it like in my example. – Sergiu N Jan 18 '16 at 10:51
  • Another way: http://stackoverflow.com/questions/17869964/replacing-string-within-php-file – mplungjan Jan 18 '16 at 10:52
  • Don't reinvent the wheel, use one of the many readily available solutions. (e.g. [twig](http://twig.sensiolabs.org/), [smarty](http://www.smarty.net/)) – Yoshi Jan 18 '16 at 11:00
  • @Yoshi Yep, smarty looks like what I'm looking for. Please post it as an answer so I can accept it. – Sergiu N Jan 18 '16 at 11:06
  • long discussion, and got a solution.. from this question read it, i have updated my answer... http://stackoverflow.com/questions/17869964/replacing-string-within-php-file ... and dont forget to appreciate this person.. he deserves for it. – devpro Jan 18 '16 at 11:08
  • this person deserve for appreciation, don't forget.. – devpro Jan 18 '16 at 11:15
  • Nope, it's not what I'm looking for. I still need to modify the HTML. – Sergiu N Jan 18 '16 at 11:35
  • What do u mean, still inprogress?? – devpro Jan 18 '16 at 12:38

5 Answers5

1

You can use the following code to achieve the desired result:

<?php
$string = "TEST";
$doc = file_get_contents('themes/default/test.html'));
echo preg_replace('/\{([A-Z]+)\}/', "$$1", $doc);
?>

P.S. Please note that it will assume that every string wrapped in { } has a variable defined. So No error checking is implemented in the code above. furthermore it assumes that all variables have only alpha characters.

Saqib Amin
  • 1,171
  • 7
  • 16
0

It's simple to use echo.

<html>
  <p>{<?php echo $string;?>}</p>
</html>

UPDATE 1:

After reading so many comments, found a solution, try this:

$string = "TEST";

$template = file_get_contents('themes/default/test.html', FILE_USE_INCLUDE_PATH);

$page = str_replace('{$string}',$string,$template);

echo $page;
devpro
  • 16,184
  • 3
  • 27
  • 38
  • Inside themes/default/test.html there is a {$string} it will of course not be rendered as PHP when it is retrieved using file_get – mplungjan Jan 18 '16 at 10:47
  • I know, but I have to use it like that. I've seen a script before that displayed variables as in my example, but I don't know how to do it. I do not have to use `file_get_contents`, but I have to retrieve the content somehow. – Sergiu N Jan 18 '16 at 10:47
  • Yes, it's working, but I still need to know the variables when I'm reading the HTML. – Sergiu N Jan 18 '16 at 11:10
0

All you PHP must be in a <?php ?> block like this:

<html>
  <p><?php echo "{" . $string . "}";?></p>
</html>
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
0

If it is possible to save your replacees in an array instead of normal variables you could use code below. I'm using it with a similar use case.

function loadFile($path) {
    $vars = array();
    $vars['string'] = "value";

    $patterns = array_map("maskPattern", array_keys($vars));
    $result = str_replace($patterns, $vars, file_get_contents($path));

    return $result;
}

function maskPattern($value) {
    return "{$" . $value . "}";
}
Alex
  • 789
  • 1
  • 12
  • 29
  • Yes, but I might have an object with many many variables, so I will need to display them like that: `{$object->variable}`, so this will not work. – Sergiu N Jan 18 '16 at 10:57
0

If you know the variable to replace in the html you can use the PHP function 'str_replace'. For your script,

$string = "TEST";

$content = file_get_contents('test.html');
$content = str_replace('{$string}', $string, $content);

echo($content);
Babitha
  • 410
  • 5
  • 8
  • I don't know it. It might be a `$variable` type or retrieved from an object like that: `$object->variable`. – Sergiu N Jan 18 '16 at 11:02