HTML to PHP converter

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

sametch
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Jul 03, 2002 3:10 am

HTML to PHP converter

Postby sametch » Fri Nov 01, 2002 1:43 am

Does anyone know of a utility that can convert a page written in HTML into PHP with echo statements.

e.g.

<tr>
<td>
<img src="myfile.jpg">
</td>
</tr>

would become:

echo "<tr>";
echo "<td>";
echo "<img src=\"myfile.jpg\">";
echo "</td>";
echo "</tr>"

at the press of a button?

sametch

DoppyNL

Postby DoppyNL » Fri Nov 01, 2002 1:47 am

Why do you want to do that?

If you're planning on using some php-code in your file you simply have to change the extention to .php and the file will be procesed by the php-enginge.
At the place's you want to use some php code you enter php-mode and when you're done you go back to html-mode, like this:

Code: Select all

Bunch of html-code
<?php
print('Now we are in PHP-mode and can do stuff with php');
?>
Back to html-mode


Get the idea?

Greetz Daan

mjr
New php-forum User
New php-forum User
Posts: 18
Joined: Fri Sep 27, 2002 6:35 am
Contact:

Re: HTML to PHP converter

Postby mjr » Fri Nov 01, 2002 9:18 am

sametch wrote:Does anyone know of a utility that can convert a page written in HTML into PHP with echo statements.

e.g.

<tr>
<td>
<img src="myfile.jpg">
</td>
</tr>

would become:

echo "<tr>";
echo "<td>";
echo "<img src="myfile.jpg">";
echo "</td>";
echo "</tr>"

at the press of a button?

sametch


like this? :D

$s=$___html_text___;
$s=str_replace('"','"',$s);
$s=preg_replace('/^(.*?)$/m',"echo "\\1";",$s);
echo $s;

DoppyNL

Postby DoppyNL » Fri Nov 01, 2002 9:53 am

Again, there is not really any use for doing this.
Its very easy to switch between html and php mode so you can put in your php code very easily.
When you want to use a lot of php-code in it or when you have to calculate something for nearly every table-cell. Then you are allready working in the file and it should be quite simple and not much work to change it to echo's by hand.

Also, I think that using a lot of echo's in your script, instead of one big html-chunk, is not very good for performance and may slow down your script. I've seen scripts with 30 echo-lines and 3 lines of "normal" php code!!!
php-code has to be checked for error's before the server starts running it! normal html-code doesn't have to be checked for errors!

In short, I think you're better of using normal html and turn to php-mode where neccesary. When using a lot of php-code it might be usefull to completely change to php, but then you're allready doing a lot and it should be done reasonably quick.

Greetz Daan

Note: I may sound a bit rude in this post, I just don't know how to make my point in a more civel way.

Jay

Postby Jay » Fri Nov 01, 2002 1:20 pm

I don't see any reason why this would need to be done, but if you wanted to you could enclose a large HTML chunk as a variable in a HEREDOC variable which works like this:

$var = <<<YOUR_DELIMITER
blah blah blah blah blach
YOUR_DELIMITER;

And that will store everything as $var. The ending delimiter has to be on it's own line, in column 0 (ie no tab spacing).

sametch
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Jul 03, 2002 3:10 am

Postby sametch » Sat Nov 02, 2002 1:51 am

Maybe I am missing something really obvious, because the consensus seams to be this is a waste of time.

What I am trying to acheive is a php page design which has four completely different layouts each comprising hundreds of lines of HTML.

The final page HTML generated by the php page will depend upon the value of a variable passed to to page (but will obviously be one of the four). I assumed to acheive this all HTML code would need to be echoed. None echoed code would appear no matter which value of variable was passed.

If there is an easier way of acheiving this outcome that I don't know about that would be great.

Jay

Postby Jay » Sat Nov 02, 2002 2:19 am

Use templating, or even simpler (as a temporary solution) store each version of the page as a separate file in a subfolder, depending on one of the 4 options you can include the page by reading the file and printing it out. The point we're trying to make is, if you've got hundreds of lines of pre-written HTML, it's easier to output it as HTML rather than PHP, because PHP allows this. You can jump in and out of PHP as you choose. For instance:

Code: Select all

<?
if ($option1) {
    ?>
    <html>
    <head>
    <title>You picked option 1</title>
    </head>

    <body>
    This is the body for option 1
    </body>
    </html>
    <?
} else {
    ?>
    <html>
    <head>
    <title>You picked something other than option 1</title>
    </head>

    <body>
    This is the body for whatever you picked
    </body>
    </html>
    <?
}
?>

Try experimenting and see which works best for you. Don't forget that it has to be easily maintainable for you as well, so if they're large files, you might want to store them in 4 sub-files and include them when needed!

It's also worth remembering that PHP parses a PHP file EVERY time before it executes (although there is a facility so it does it once, which speeds up your pages by a factor of 10), so if it's managing large files which contain 75% useless stuff (as the user will only get one result of 4) it's taking up processor time for no reason.

BTW, if you really want HTML written up as PHP, just do this:

Code: Select all

<?
$oldFile = file("thefile.html");
while(list($lineNo,$line) = each($oldFile)) {
    $newFile[] = "print '".str_replace(array("\\","'"),array("\\\\","\\'"),$line)."';";
}
$newFile = implode("\n",$newFile);
?>

sametch
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Jul 03, 2002 3:10 am

Postby sametch » Mon Nov 04, 2002 11:39 pm

thanks Jay, thats what I am looking for

sametch :)


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest