Parsing/Passing HTML via 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
JasonStockman
Forum Newbie
Posts: 2
Joined: Fri Dec 12, 2008 5:00 pm

Parsing/Passing HTML via PHP

Post by JasonStockman »

Hello everyone,

I'm a very savvy programmer, and a fast learner, but I'm in need of some help here. :banghead:

What I would like to do is have a PHP page that is able to extract the contents/code/source of a separate HTML page, modify it slightly, and output/echo the result (without modifying the original). I would like this PHP page to be able to apply these modifications to several different HTML pages when called upon.

The overall layout will involve 3 pages at any given time.

index.html (page with the link calling upon page.php in a pop-up window)
page.php (the little guy doing all the work)
example.html (the page containing the source we want copied/modified)

index.html will contain a link to a pop-up window, the contents of which will be the altered form of example.html

All I really need is a push in the right direction with this. I thought of passing the link to the 3rd page, the page to be modified, as an argument in the URL to page.php - something like this:

Code: Select all

<a href="page.php?link=example.html">See modified version in a pop-up window!</a>
The task of passing example.html as a variable seems easy enough, but actually doing anything with it (parsing it I guess you would say) has left me posting here. Is PHP capable of doing this, or will I need the support of some 3rd party plugins?

Thanks for your help, everyone! :drunk:

-Jason
cavemaneca
Forum Commoner
Posts: 59
Joined: Sat Dec 13, 2008 2:16 am

Re: Parsing/Passing HTML via PHP

Post by cavemaneca »

Maybe like this?
page.php

Code: Select all

<?php
$name=$_GET['name'];
$file=fopen('example.html','r');
$message=fread($file,filesize('example.html'));
fclose($file);
 
$message=str_replace('%%NAME%%',$name,$message);
echo $message;
?>
 
example.html

Code: Select all

<html>
<head>
</head>
<body>
Hello! My Name is %%NAME%%! How Are You?
</body>
</html>
Calling /page.php?name=whatever will put that variable in the page instead of %%NAME%% and although this is pretty limited, if you had specific areas outlined for editing in the document you could do things like writing the whole page. Also it would be simple to write the output to a new file if that's what you need. Either way this only works well if example.html is a model page, and not a page someone would actually look at.

As for using different pages, just change it to this

Code: Select all

<?php
$name=$_GET['name'];
$link=$_GET['link'];
$file=fopen($link,'r');
$message=fread($file,filesize($link));
fclose($file);
 
$message=str_replace('%%NAME%%',$name,$message);
echo $message;
?>
I hope this helps you with what you need! You weren't really that specific with your request.(I didn't understand if you just wanted to edit any page or use a blank model like I used)
JasonStockman
Forum Newbie
Posts: 2
Joined: Fri Dec 12, 2008 5:00 pm

Re: Parsing/Passing HTML via PHP

Post by JasonStockman »

Worked like a charm, thank you.
Post Reply