ajcrm125 Posted March 9, 2009 Share Posted March 9, 2009 I have a php file that is grabbing an html file and eval'ing it, looking for any php that may be embedded within it. In the html file I currently am defining a variable as follows: <?php $variable = "some value"; ?> ... a bunch of html stuff.... But when I eval the file, I get: Parse error: parse error, unexpected $ in C:\Program Files\xampp\htdocs\mapleridge\index.php(3) : eval()'d code on line 1 And just for kicks, I removed the php code from the html file and got the same result. Am I supposed to be using some other means to parse the html? I'm not worried about the html content... I'm saving it and displaying it later.. but I'm sorta looking for a pre-process solution. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/ Share on other sites More sharing options...
samshel Posted March 10, 2009 Share Posted March 10, 2009 eval () works only for PHP code not HTML. i think u can achieve what u want using just simple include(). include ur file wherever u want and the code will be executed perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-780767 Share on other sites More sharing options...
ajcrm125 Posted March 10, 2009 Author Share Posted March 10, 2009 eval () works only for PHP code not HTML. i think u can achieve what u want using just simple include(). include ur file wherever u want and the code will be executed perfectly. Yeah, I tried that. What happens is that when you include an html file the contents are echoed directly to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-780785 Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 ob_start ob_get_contents ob_end_clean Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-780790 Share on other sites More sharing options...
ajcrm125 Posted March 10, 2009 Author Share Posted March 10, 2009 ob_start ob_get_contents ob_end_clean Yep, tried that too. What comes out of the get_contents function is only the html. The php never makes it out and within the function, never gets evaluated. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-780875 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 can you post the code you say you have tried as we might be able to wright it different and surprise you. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-780877 Share on other sites More sharing options...
ajcrm125 Posted March 10, 2009 Author Share Posted March 10, 2009 Okay... found out some more interesting info. using 'include' actually does work, but only within that php file (or perhaps it's the function) Basically, I have a function in an external utils.php file that does this: function GetHTMLContent($pageName) { if (file_exists($pageName)) { ob_start(); include($pageName); $content = ob_get_contents(); ob_end_clean(); return $content; } else { die("Page " . $pageName . " does not exist."); } } Works great and returns the html content. Now if I add a quick 'echo' within this function to display any variables defined within the embedded php, it works and can see them. If I add the echo in the index.php file (that includes utils.php and calls out this function) after the function of course, it doesn't show and the variables are undefined. I'm betting that if I placed this function code within my index.php it would work fine, but I;d really like to have it in a separate file to better manage this project. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-781154 Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 How are you using this function on the index page? Because you do have to call it and set a value to return the contents. Example: index.php include('utils.php'); $content = GetHTMLContent('mypage.php'); echo $content; Should work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-781159 Share on other sites More sharing options...
ajcrm125 Posted March 11, 2009 Author Share Posted March 11, 2009 Check this out.... firstFile.php (think of this as index.php) <?php include("utils.php"); // let's see if we can parse the embedded php within this html if (!$page) echo "page variable is not defined<br>"; else echo "page variable is defined as " . $page . "<br>"; // now dump the HTML portion of the HTML file $content = GetHTMLContent("secondFile.html"); echo $content; if (!$page) echo "page variable is still not defined<br>"; else echo "page variable is defined as " . $page . "<br>"; ?> utils.php: <?php // Put commonly used functions here function GetHTMLContent($pageName) { if (file_exists($pageName)) { ob_start(); include($pageName); $content = ob_get_contents(); ob_end_clean(); if (!$page) echo "page variable is still not defined within utils.php<br>"; else echo "page variable is defined within utils.php as " . $page . "<br>"; return $content; } else { die("Page " . $pageName . " does not exist."); } } ?> secondFile.html: <?php $page = "someValue"; ?> <h1> Some header text</h1> Contents of the HTML file go here<p> If I load up firstFile.php I get the following result: page variable is not defined page variable is defined within utils.php as someValue Some header text Contents of the HTML file go here page variable is still not defined So as soon as I leave utils.php the variable is no longer defined. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-781736 Share on other sites More sharing options...
trq Posted March 11, 2009 Share Posted March 11, 2009 Because include places the contents of the file into the same scope of the line it is called on. eg; If you call include within a function any variables that include contains are only available within that function. You might be better of describing what your trying to do in broader terms, you likely need to take a different approach. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-781737 Share on other sites More sharing options...
ajcrm125 Posted March 11, 2009 Author Share Posted March 11, 2009 Because include places the contents of the file into the same scope of the line it is called on. eg; If you call include within a function any variables that include contains are only available within that function. You might be better of describing what your trying to do in broader terms, you likely need to take a different approach. Ah ok..... What I'm trying to do is allow my index.php to display a template, whose center content is taken from a second html or php file (usually an html file with a table in it). One of the features I wanted to add was allowing the main banner (image) of the page to change depending on the content of this file. If no banner was defined, then the default one would be used. Quote Link to comment https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/#findComment-781935 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.