Page 1 of 1
Parsing php code from lines read with file()
Posted: Sun Oct 16, 2005 11:33 am
by DanMan101
Hello-
Here is my situation:
I have an html template file.
template.htm:
Code: Select all
...
<!--begin-->
<title><?php echo $page_title ?></title>
<!--end-->
...
I have a php script that wants to display selected lines from template.htm, such as anything between the begin and end comments.
test.php
Code: Select all
$lines = file('template.htm');
$page_title = "My Page Title";
foreach ($lines as $line) {
... // detect if line within begin and end comments
if ($withinTags) {
echo $line;
}
}
The resulting page will have a page title of "<?php echo $page_title ?>" instead of the desired "My Page Title".
How do I get it to parse the php code within the $line string?
Thanks for the help,
DanMan101
Re: Parsing php code from lines read with file()
Posted: Sun Oct 16, 2005 12:24 pm
by alex.barylski
DanMan101 wrote:Hello-
Here is my situation:
I have an html template file.
template.htm:
Code: Select all
...
<!--begin-->
<title><?php echo $page_title ?></title>
<!--end-->
...
I have a php script that wants to display selected lines from template.htm, such as anything between the begin and end comments.
test.php
Code: Select all
$lines = file('template.htm');
$page_title = "My Page Title";
foreach ($lines as $line) {
... // detect if line within begin and end comments
if ($withinTags) {
echo $line;
}
}
The resulting page will have a page title of "<?php echo $page_title ?>" instead of the desired "My Page Title".
How do I get it to parse the php code within the $line string?
Thanks for the help,
DanMan101
Well the easiest way would be instead of using HTML files use a PHP file and call
include() instead of
file()
Note: You would need to use output buffering to prevent the include from sending output to the screen immediately:
Code: Select all
ob_start();
include_once($file);
$contents = ob_get_contents();
ob_end_clean();
echo $contents;
The second "much more difficult" way of doing this would be to parse the HTML file, extracting only code between PHP tags - running that code through
eval() and replacing anything between <?php ?> tags with it's executed result.
Good luck with that one!!!
I would suggest using the first method I mentioned
Hopefully I answered what you were asking...i'll be honest I didn't qwuite get the question
Cheers

Posted: Sun Oct 16, 2005 12:26 pm
by feyd
setting up your variables first, then using
include() will be easier to do
Re: Parsing php code from lines read with file()
Posted: Sun Oct 16, 2005 12:43 pm
by DanMan101
Hockey wrote:
Well the easiest way would be instead of using HTML files use a PHP file and call
include() instead of
file()
Note: You would need to use output buffering to prevent the include from sending output to the screen immediately:
Code: Select all
ob_start();
include_once($file);
$contents = ob_get_contents();
ob_end_clean();
echo $contents;
The second "much more difficult" way of doing this would be to parse the HTML file, extracting only code between PHP tags - running that code through
eval() and replacing anything between <?php ?> tags with it's executed result.
Good luck with that one!!!
I would suggest using the first method I mentioned
Hopefully I answered what you were asking...i'll be honest I didn't qwuite get the question
Cheers

Thanks for the reply. Now, by using include and output buffering, how would I just output the lines between the two comments in the template file? The file() returned an array with each line being an element, but $contents is a string containing the entire output, correct?. I'm sorry for the dumb questions, but can I convert the $contents string to an array similar to that returned by file() (each element being a line)?
Thanks.
Re: Parsing php code from lines read with file()
Posted: Sun Oct 16, 2005 12:56 pm
by alex.barylski
DanMan101 wrote:Hockey wrote:
Well the easiest way would be instead of using HTML files use a PHP file and call
include() instead of
file()
Note: You would need to use output buffering to prevent the include from sending output to the screen immediately:
Code: Select all
ob_start();
include_once($file);
$contents = ob_get_contents();
ob_end_clean();
echo $contents;
The second "much more difficult" way of doing this would be to parse the HTML file, extracting only code between PHP tags - running that code through
eval() and replacing anything between <?php ?> tags with it's executed result.
Good luck with that one!!!
I would suggest using the first method I mentioned
Hopefully I answered what you were asking...i'll be honest I didn't qwuite get the question
Cheers

Thanks for the reply. Now, by using include and output buffering, how would I just output the lines between the two comments in the template file? The file() returned an array with each line being an element, but $contents is a string containing the entire output, correct?. I'm sorry for the dumb questions, but can I convert the $contents string to an array similar to that returned by file() (each element being a line)?
Thanks.
When used with output buffering will return the results of the entire file, correct!!!
Anything between PHP tags will be parsed and processed - HTML will be returned as normal.
But why would you need to only return that between HTML comments???
Sorry, still not totally clear on your problem
Re: Parsing php code from lines read with file()
Posted: Sun Oct 16, 2005 1:03 pm
by alex.barylski
DanMan101 wrote:Hello-
Here is my situation:
I have an html template file.
template.htm:
Code: Select all
...
<!--begin-->
<title><?php echo $page_title ?></title>
<!--end-->
...
I have a php script that wants to display selected lines from template.htm, such as anything between the begin and end comments.
test.php
Code: Select all
$lines = file('template.htm');
$page_title = "My Page Title";
foreach ($lines as $line) {
... // detect if line within begin and end comments
if ($withinTags) {
echo $line;
}
}
The resulting page will have a page title of "<?php echo $page_title ?>" instead of the desired "My Page Title".
How do I get it to parse the php code within the $line string?
Thanks for the help,
DanMan101
I think I see what your trying to do now...
Basically...use the code you have, but instead of
file() use
include()
Now just use one of PHP's string functions like
explode() to turn the parsed/processed template into an array of lines...
Code: Select all
$arr_of_lines = explode('/n', $contents);
Depending on which system (Windows, *nix, Mac, etc) the seperator string may vary...
For instance on Windows CRLF (10+13 or "/r/n") would be required to break a string into lines...however on Linux I believe it's just "/n"
so...
Code: Select all
$arr_of_lines = explode('/n', $contents);
Should work...
That is if memory serves me correctly
Cheers

Posted: Sun Oct 16, 2005 1:07 pm
by feyd
\n not /n, etc etc
and double quote strings will parse the metacharacter, single quote ones will not.
Posted: Sun Oct 16, 2005 2:31 pm
by DanMan101
Thanks a lot, Hockey and feyd. It works exactly as desired now. You guys are great!
Regards,
DanMan101