Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/148687-trying-to-parse-php-from-html-file/
Share on other sites

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. :(

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.

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.

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. :(

 

 

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.

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.

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.