Parsing a text file into PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Parsing a text file into PHP

Post 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>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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
Post Reply