Jump to content

parse html document with snippets of php code


Danny620

Recommended Posts

You need to request the file from a remote location e.g. instead of the local filesystem use

 

<?php

$myRemoteContents = file_get_contents( 'http://localhost/myfile.php' );

 

If you try and access a local file from the filesystem then you will get the pre processed version of it (with PHP source in).

 

If that's what your after.

Link to comment
Share on other sites

The answer is no. If you access the file from the filesystem as AGuyWithAthing stated you get the preprocessed file with the PHP content and it would not be a properly formatted document to process with DOM. And, if you access the file via an http request you will not have the PHP code (assuming that you want to keep that in tact).

 

However, it would be beneficial for you to explain exactly what you are trying to do. It sounds as if you may be taking a more complicated approach than you need to.

Link to comment
Share on other sites

its weird cos i still get code like this

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

<?php //////////////////////SEO TOOL///////////////////////////

 

$title = 'Green Deal Nationwide - PB Energy Solutions Ltd';

$description = 'Delivering all your environmental needs to \'green\' up your business, improve reputation, increase profitability and give a competitive advantage.';

 

///////////////////////////////////////////////////////

 

?><?php include('includes/settings.php'); ?><?php include('includes/header.php'); ?><html><body><div class="container">

 

        <div id="large-page-img">

        <img src="<?php%20echo%20URL();%20?>images/home-page-slide.jpg" width="911" height="230"><img src="<?php%20echo%20URL();%20?>images/home-page-slide-1.jpg" width="911" height="230"><img src="<?php%20echo%20URL();%20?>images/home-page-slide-2.jpg" width="911" height="230"></div>

        <div id="content-home">

 

but the one in bold hasn't worked properly yet the php code at the top is fine

Link to comment
Share on other sites

My idea is to load a php file that contains both html and php code then use the dom to extract and edit some of the div content like <div class="cms-editable"></div> then use the dom to write the file back with the new text in the divs and the php code still intact

 

its like a cms system like pagelime and cushycms

Link to comment
Share on other sites

its doing my head in all i want to do really is

 

load a php/html file that contains both php and html

 

use dom to extract info out of the <div class="edit-me">text that can be edited</div>

 

then use dom to modify the <div class="edit-me">this text has been changed</div>

 

then write the nearly modified dom back to file keeping the php code as well

Link to comment
Share on other sites

its doing my head in all i want to do really is

 

load a php/html file that contains both php and html

 

use dom to extract info out of the <div class="edit-me">text that can be edited</div>

 

then use dom to modify the <div class="edit-me">this text has been changed</div>

 

then write the nearly modified dom back to file keeping the php code as well

 

You cannot do that - it is not a DOM compliant document. It would have to be a rendered HTML document to do that. If you have content that needs to be modified, then you should store that content in a database. Or, you could even put the editable text into separate flat text files that you could read/modify.

 

Or, if you really, really want to take this approach you could build a complex process to do the following:

 

1. Load the file via an http request.

2. Use DOM to find the DIV content you want to modify and save it to a variable ($original)

3. Make the modifications you want to the content and save to a different variable ($new)

4. Open the same file via the file system and load the contents into a variable ($content)

5. Do a str_replace() using str_replace($original, $new, $content)

6. Write the new, modified contents back to the file

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.