Using template file : str_replace « String « PHP






Using template file

 
<html>
<head><title>{title}</title></head>
<body>
<h1>{headline}</h1>
<h2>By {byline}</h2>

{article}

<hr/>
<h4>Page generated: {date}</h4>
</body>
</html>
////////////////////////

<?php
$page = file_get_contents('article.html');
if ($page === false) {

    die("Can't read article.html: $php_errormsg");
}
$vars = array('{title}' => 'A',
              '{headline}' => 'B',
              '{byline}' => 'C',
              '{article}' => "<p>D</p>",
              '{date}' => date('l, F j, Y'));

foreach ($vars as $field => $new_value) {
    $page = str_replace($field, $new_value, $page);
}

$result = file_put_contents('dog-article.html', $page);
if (($result === false) || ($result == -1)) {

    die("Couldn't write dog-article.html: $php_errormsg");
}
?>
  
  








Related examples in the same category

1.Replacing Substrings Using str_replace()
2.str_replace demo
3.str_replace() accepts arrays for all its arguments
4.str_replace() function searches for occurrence in string, replacing all instances with replacement.
5.str_replace.php
6.Using str_replace()
7.mixed str_replace ( mixed needle, mixed replace, mixed haystack [, int &count] )
8.Replacing Tabs with Spaces
9.case-sensitive?
10.Switching tabs and spaces