/    Sign up×
Community /Pin to ProfileBookmark

Output buffering

I’m trying to use php’s output buffering functions. But I haven’t really succeeded yet. I have a txt-file with the contents:

test.txt:
<font color=”#FF0000″><b>$var</b></font>

I’m trying to capture what’s in here then assign a value to $var and print it. This is my code so far.

test.php:
<?php
$var = “It works!”;
ob_start ();
include (‘test.txt’);
$test = ob_get_contents ();
ob_clean ();
ob_end_flush ();

echo “$test”;
?>

It prints out (viewing source code):
<font color=”#FF0000″><b>$var</b></font>

So the variable $var hasn’t gotten his value.

Thanks in advance.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@PittimannDec 21.2003 — Hi!

There is no php in your test.txt, so $var is just a string.

This works:

test.txt:
[code=php]
<?PHP
echo "<font color="#FF0000"><b>$var</b></font>";
?>
[/code]

test.php:
[code=php]
<?php
$var = "It works!";
include ('test.txt');
ob_start ();
$test = ob_get_contents ();
ob_clean ();
ob_end_flush ();
echo $test;
?>
[/code]

Cheers - Pit
×

Success!

Help @Bootsman123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...