fuston05 0 Junior Poster in Training

I am making a php calendar script. I want to to store events on separate text files. Beside my calendar is an iframe which is where it will read the events. I cant figure out how to make php read just the portion that is the event, not the code on the page too. heres the "day_1.php that is linked to the 1st of the month. I am new to programming so..

<h1><?php echo $_GET["day"] . "</h1>"; ?><br /><h3>Today's Events:</h3>
<?php $event= "This where content to remember will be added..asd asd  asdas fa sd etc..." ;?>
<p><?php echo $event;  ?></p>

<div id= "add_content"
    <a href= "add_content.php"><span class= "underline">Add Content</span></a>
</div>

so if i use fopen and read, it reads all the code back to me as well. any advise?
I know i could use mysql, but i want this to be php, and text files only for now.

Dani AI

Generated

— fopen reads the raw file on disk (the PHP source and HTML), not the page after PHP has run. That is why you see the code. There are three simple, reliable ways to keep code and data separate so your iframe shows only the event text.

Keep data out of view files: store events in plain text, JSON, or a small PHP file that returns data. This makes reading/parsing trivial and safe. Example: put a JSON file per day and decode it.

$events = json_decode(file_get_contents(__DIR__ . '/events/day_1.json'), true);
// $events is now an array you can loop and display

Use a PHP data file that returns a value. The included file executes but does not directly print anything:

$events = include __DIR__ . '/events/day_1.php'; // day_1.php returns an array

If you must extract content from an existing page that contains HTML and PHP output, grab the rendered output (not the source) and parse it. Either request the page via its URL with file_get_contents('http://...') or include it while buffering output, then use DOMDocument or a small regex to pull the event portion:

ob_start();
include __DIR__ . '/day_1.php';
$html = ob_get_clean();
// parse $html for the event block using DOMDocument or string functions

References for the functions above: file_get_contents, include, and json_decode.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.