1

I would like to replace several text in a PHP file before I include it in my index.php

For example. The file that I'm about to include in my index.php is a template file called "main.php" and it contains code something like below:

<html>
<head>
<script src="js/jquery.min.js"></script>
<script src="js/jquery-migrate.min.js"></script>
</head>
<body>
Hi, [your_name]. Welcome to [company_name]. Enjoy your stay here!
</body>
</html>

and on my index.php I will have a bunch of queries to get data from database and put it into main.php file. Example below:

<?php
$sql_stmt = "select name, company FROM comp_info WHERE deleted='0' AND status='2'";
$sql = mysqli_query($conn, $sql_stmt) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($sql);

include_once("main.php");
?>

I tried to use this method below, but I dont know how to use it in this case:

str_replace("[your_name]", $row['name'], main.php);
str_replace("[company_name]", $row['company'], main.php);

So far, what I've searched from the internet is this : Replacing {{string}} within php file But it doesn't works for this case.

3 Answers 3

2

Why reinvent the wheel?

Either pick an existing template engine, or just use vanilla php:

<?php
$sql_stmt = "select name, company FROM comp_info WHERE deleted='0' AND status='2'";
$sql = mysqli_query($conn, $sql_stmt) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($sql);

$name = $row['name'];
$company = $row['company'];

include_once("main.php");
?>

Template (main.php):

<html>
<head>
<script src="js/jquery.min.js"></script>
<script src="js/jquery-migrate.min.js"></script>
</head>
<body>
Hi, <?=$name?>. Welcome to <?=$company?>. Enjoy your stay here!
</body>
</html>

If you really want to create your own half baked template system, then you would need to use output buffering to capture the contents of the include in a string, then perform your replacements:

<?php
$sql_stmt = "select name, company FROM comp_info WHERE deleted='0' AND status='2'";
$sql = mysqli_query($conn, $sql_stmt) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($sql);


ob_start();
include_once("main.php");
$mainString = ob_get_clean();

str_replace("[your_name]", $row['name'], $mainString);
str_replace("[company_name]", $row['company'], $mainString);

echo $mainString;
?>
Sign up to request clarification or add additional context in comments.

Comments

0

create a sting called {%name%} store into content were ever you want then you replace with {%name%} with actual value when you trying to display.

Comments

0

try this

 <?php
$sql_stmt = "select name, company FROM comp_info WHERE deleted='0' AND status='2'";
$sql = mysqli_query($conn, $sql_stmt) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($sql);


ob_start();
include_once("main.php");
$mainString = ob_get_clean();

strip_tags($row['name']);
strip_tags($row['company']);

echo $mainString;
?>

Comments

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.