Danny620 Posted December 22, 2011 Share Posted December 22, 2011 Is it possible to parse html document with snippets of php code in them using DOMDocument? i.e load html from file then parse/change them with DOMDocument and then save them back to file I have tryed but i get <?php%20echo%20URL();%20?> Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/ Share on other sites More sharing options...
AGuyWithAthing Posted December 22, 2011 Share Posted December 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300679 Share on other sites More sharing options...
Psycho Posted December 22, 2011 Share Posted December 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300683 Share on other sites More sharing options...
Danny620 Posted December 22, 2011 Author Share Posted December 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300685 Share on other sites More sharing options...
Danny620 Posted December 22, 2011 Author Share Posted December 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300687 Share on other sites More sharing options...
AGuyWithAthing Posted December 22, 2011 Share Posted December 22, 2011 You could just run the code inside an output buffer. e.g. <?php ob_start(); include( 'myfile.php' ); $myFileContents = ob_get_contents(); ob_end_flush(); This will execute the contents of your php file and capture the required output without having to remotely access it. Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300691 Share on other sites More sharing options...
Danny620 Posted December 22, 2011 Author Share Posted December 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300697 Share on other sites More sharing options...
Danny620 Posted December 22, 2011 Author Share Posted December 22, 2011 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300715 Share on other sites More sharing options...
Psycho Posted December 23, 2011 Share Posted December 23, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253714-parse-html-document-with-snippets-of-php-code/#findComment-1300724 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.