Page 1 of 1

Parsing a text file into PHP

Posted: Tue Sep 23, 2003 11:33 am
by php_wiz_kid
How would I parse a text file into php. Here's an example of what I want.

constants.php:
<?php
define(PAGE_BODY, 'The body was displayed correctly');
?>

index.txt:
<html>
<head>
<title>INDEX</title>
</head>
<body>
{PAGE_BODY}
</body>
</html>

I want a class to be able to parse index.txt into a PHP file so that when I load index.txt into a php document it will display the contents of index.txt and the contents of PAGE_BODY which was defined in antoher php document. So it will end up looking like this through the source:

index.php:
<head>
<title>INDEX</title>
</head>
<body>
The body was displayed correctly
</body>
</html>

Posted: Tue Sep 23, 2003 12:45 pm
by McGruff
(1) fopen/fread to get index.txt into a string
(2) define the replacement string (perhaps using output buffering - depends)
(3) str_replace your page body placeholder with the replacement string