Parsing php code from lines read with file()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DanMan101
Forum Newbie
Posts: 3
Joined: Sun Oct 16, 2005 11:16 am

Parsing php code from lines read with file()

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Parsing php code from lines read with file()

Post 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 :oops:

Cheers :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

setting up your variables first, then using include() will be easier to do
DanMan101
Forum Newbie
Posts: 3
Joined: Sun Oct 16, 2005 11:16 am

Re: Parsing php code from lines read with file()

Post 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 :oops:

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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Parsing php code from lines read with file()

Post 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 :oops:

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.

Code: Select all

include('template.tpl.php')
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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Parsing php code from lines read with file()

Post 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... :oops:

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 8)

Cheers :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

\n not /n, etc etc

and double quote strings will parse the metacharacter, single quote ones will not.

Code: Select all

var_export("\n" == '\n');
DanMan101
Forum Newbie
Posts: 3
Joined: Sun Oct 16, 2005 11:16 am

Post by DanMan101 »

Thanks a lot, Hockey and feyd. It works exactly as desired now. You guys are great!

Regards,
DanMan101
Post Reply