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.

11
  • 1
    Possible duplicate of PHP replace string after using file_get_contents Commented Jan 18, 2016 at 10:48
  • 1
    Checkout this Question on SO: stackoverflow.com/questions/15065387/… Commented Jan 18, 2016 at 10:48
  • e.g. echo str_replace('{$string}', $string, file_get_contents('themes/default/test.html')); Commented Jan 18, 2016 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. Commented Jan 18, 2016 at 10:51
  • Another way: stackoverflow.com/questions/17869964/… Commented Jan 18, 2016 at 10:52

5 Answers 5

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.

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

1 Comment

Perhaps mention that $$1 will replace whatever is found in {} with the contents of the var found
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 . "}";
}

1 Comment

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

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

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

11 Comments

But it is not in PHP, it is in HTML
Hi! Yes, that's a simple solution, but I have to only use brackets and variable inside. I've seen a script that displayed the variables like in my example before, so I know it's possible.
@mplungjan i don't get it :/.
@user3284463 He is trying to build a mini-templating engine that will automatically replace values wrapped in {} with variables (if they exist)
The {$string}] is in the html file he gets using file_get
|
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);

1 Comment

I don't know it. It might be a $variable type or retrieved from an object like that: $object->variable.
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;

3 Comments

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
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.
Yes, it's working, but I still need to know the variables when I'm reading the HTML.

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.