0

Good day.

We have test.php with next code:

<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
<div class="container">
<p class="test">{Text}</p>
</div>
<!-- STYLE_TEST1 and STYLE_TEST2 - set in define as "/css/styletest1.css" and "/css/styletest2.css" -->

and index.php with code:

$content = file_get_contents("test.php");
$content = ereg_replace("{Text}", 'hello', $content);
echo $content;

in result we get code:

<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
<div class="container">
<p class="test">{Text}</p>
</div>

we have problem with styles, becose echo $content; print <?php echo STYLE_TEST1; ?> and <?php echo STYLE_TEST2; ?> and not replace on values which set in define for this elements(not change on "/css/styletest1.css" and "/css/styletest2.css").

Tell me please how right make replace?

P.S.: we need that styles was include in file test.php, ie file test.php should have lines:

<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
6
  • you are basically echoing php script... you can't do that... that's like this echo '<?php echo 'hi';?>'; Commented Mar 25, 2014 at 22:13
  • possible duplicate of PHP file_get_contents with php intact? Commented Mar 25, 2014 at 22:14
  • @Anonymous tell mep please how replace my code if use ob ? Commented Mar 25, 2014 at 22:23
  • You take the returned value and replace the values that you desire. Commented Mar 25, 2014 at 22:25
  • @Anonymous i right understand - pastebin.com/ihT6bGpZ ? Commented Mar 25, 2014 at 22:31

2 Answers 2

1

Thanks all for help!

This is the best decision for me on my question

function getTemplate($file) {

    ob_start(); // start output buffer

    include $file;
    $template = ob_get_contents(); // get contents of buffer
    ob_end_clean();
    return $template;

}

$content = getTemplate("test.php");
$content = ereg_replace("{Text}", 'hello', $content);
echo $content;
Sign up to request clarification or add additional context in comments.

Comments

0

A way to fix this is to make it a variable....

just make sure you have

define("STYLE_TEST1", "/css/styletest1.css");
define("STYLE_TEST2", "/css/styletest2.css");

test.php

<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
<div class="container">
<p class="test">{<?=$variable?>}</p>
</div>
<!-- STYLE_TEST1 and STYLE_TEST2 - set in define as "/css/styletest1.css" and "/css/styletest2.css" -->

index.php

$variable = 'hello';

include 'test.php';

The result will be:

<link rel="stylesheet" type="text/css" href="/css/styletest1.css" />
<link rel="stylesheet" type="text/css" href="/css/styletest2.css" />
<div class="container">
<p class="test">{hello}</p>
</div>

5 Comments

see p.s. in question please
the styles are included in test.php... can you elaborate what you mean?
@learner I changed it a bit... it should still work if you defined the variables correctly...
nice, why <?= (what it is)?
@learner it's called a short open tag and it basically replaces <?php echo $this; ?> to <?=$this?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.