0

im looking for a way to put a php string into a html file, which i pull into with file_get_contents and echo then.

PHP Snipped:

if($_GET['title'] == "main"){
    $name = "Jan";
    $page = file_get_contents('pages/main.html');
    echo $page;
}

HTML Snipped:

<div id='personal_data'>
    Persönliche Daten:</br>
    Name: $name</br>
    Alter: 18</br>
    Wohnort: Keine Ahnung
</div>
vJan00
  • 13
  • 3

2 Answers2

0

I think there are similar questions on here: eg. PHP sending variables to file_get_contents() But file_get_contents is just returning a string, so str_replace should work on it.

# https://www.php.net/manual/en/function.str-replace.php
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

# your example
if($_GET['title'] == "main"){
    $name = "Jan";
    $page = file_get_contents('pages/main.html');
    echo str_replace('$name', $name, $page);
}
clash
  • 427
  • 4
  • 11
0

With file_get_contents() you're processing your HTML file like plain text file, PHP will not parse it or interpret anything from it. The echo will then print the raw content of this file.

If you're new to PHP & you're trying to load a page and interpret variables, I suggest you to take a look a some PHP introduction topics & tutorials on the web at first. Unless you're building a kind of page-caching, this will not be easy and practicle for you to maintain.

By the way, if you want to "include" HTML content from a file into your main PHP file, you have to use include() or require() functions.

Here is some example of file inclusion : https://www.tutorialspoint.com/php/php_file_inclusion.htm

Adri1
  • 311
  • 1
  • 5