html in a php variable - php

I want to put some procedural stuff like html in a variable;
Normally we do this:
<?php
$my_var = '
<h1>some header</h1>
some text.
';
?>
Now I want it to look like this:
<?php
$my_var = '
?>
<h1>some header</h1>
some text.
<?php
';
?>
So that we open a php code, declare a variable, do some stuff outside php, and then reopen php code and close the var and we get that stuff that is outside php, we get it inside that variable.
Thanks.

<?php
ob_start();
?>
<h1>some header</h1>
some text.
<?php
$my_var = ob_get_contents();
ob_end_clean();
echo my_var;
?>

i don't think there is a way because php ignores lines outside the php tags.
if u have the php in a other file user file_get_contents

Please check php heredoc
http://php.net/manual/en/language.types.string.php
Hope it helps

Related

Php file that does not parse includes

My goal is to have a file with the following html/php created on the server automatically
which it does do, however it includes the php includes as well so when the page is build it includes everything that is in the include as well.
I would want include("../includes/right.html"); to be left alone
in the page that is generated,but I want other php variables to be parsed.
<?php
// Start the buffering //
ob_start();
?>
<?php echo $name; ?>
test text line one
<?php include("../includes/right.html"); ?>
test text line two
<?php
// Putting content buffer into file //
file_put_contents('mypage.html', ob_get_contents());
?>
This is straight from the PHP manual at http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
This is called nowdoc syntax and comes with PHP 5.3. It's generally the best practice for something like this.
echo <<<'EOT'
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41
EOT;
anything between 'EOT' and EOT; will be rendered on the page as text with no parsing done whatsoever.
You need to escape that line as HTML, so it's not passed through the PHP parser, e.g.
<?php
// Start the buffering //
ob_start();
?>
<?php echo $name; ?>
test text line one
<?php include("../includes/right.html"); ?>
test text line two
<?php
// Putting content buffer into file //
file_put_contents('mypage.html', ob_get_contents());
?>

PHP Show Source Code Instead of .html File

I'm sorry if the title is a bit confusing, but this is my problem:
I need to echo the source of a .html file using PHP. I tried using PHP include(); while the file's extensions were .html.txt, but that showed up as if it is . When I access the file directly, for example "example.com/file.html.txt", it shows up as a normal .txt.
This is the PHP code I'm using to display the file:
<div>
<?php
include "../head.php";
$path = $_GET["p"];
?>
<div style="position:absolute; top:90px; padding-left:5px; padding-right:10px;">
<?php
echo "<code>";
include("files/".$path);
echo "</code>";
?>
</div>
I hope I'm understandable! :) Thanks in advance for your help!
Don't use include(), read the contents of the file and print them instead (don't forget to escape).
In your case $path is user input, you should make sure that no sensitive informatin can be accessed. Using basename($path); makes sure that no directory traversal is possible.
<code>
<?php
// $path = basename($path);
$html = file_get_contents("files/".$path);
echo htmlspecialchars($html);
?>
</code>
If you want to output HTML tags in an html file, use htmlspecialchars() to escape special characters.
I'd suggest wrapping it inside a textarea. Simple and quick.
echo "<textarea>";
include "/filename.txt";
echo "</textarea>";
Sometimes, when we're looking for something very simple, we overlook the obvious. :o)
if you want to be able to see html source in a browser, the best method would be to run it through the function htmlentities() or like. the tricky part is getting the file contents of the include into a string so that you can do that. for this we use ob_start() and ob_get_clean(). these basically start an output buffer, then get the contents of the output buffer so you can process it and echo it.
<div>
<?php
include "../head.php";
$path = $_GET["p"];
?>
<div style="position:absolute; top:90px; padding-left:5px; padding-right:10px;">
<?php
echo "<code>";
ob_start();
include("files/".$path);
echo htmlentities(ob_get_clean());
echo "</code>";
?>
</div>
Include is not safe. If your file has PHP code it will be executed.
You should rather use this:
echo htmlspecialchars(file_get_contents('yourfile.html'));

incorprating css and html elements into php

I have a bunch of php files that I want to incorporate html (divs) and css (external stylesheets) elements into.
If any one has any tips / ideas on how to go about doing this I'd love to hear them :)
You could either :
Integrate HTML code in your PHP file,
Or have your PHP script echo the HTML content.
With the first solution, your PHP file could look like this :
<?php
$var = 10;
// some PHP code
?>
<div>
this is some HTML
</div>
<?php
// and some PHP code again
?>
For more informations about this, see the following section of the manual : Escaping from HTML.
While, with the second solution, your PHP file could look like this :
<?php
$var = 10;
// some PHP code
echo '<div>this is some HTML</div>';
// and some PHP code again
?>
Basically, you are free to mix HTML and PHP code in the same PHP script : outside of <?php ... ?> tags, things will not get interpreted as PHP code.
All you need to do is exit php and write html as normal:
<?php
// php code
?>
<div>
<p>Some html</p>
</div>
<?php
// more php code
?>
Take a look at the PHP alternative syntax for control structures. It is often times more comprehensive when you start mixing with plain HTML:
<?php if(...): ?>
<p>paragraph</p>
<?php endif; ?>
Instead of:
<?php if(...) { ?>
<p>paragraph</p>
<?php } ?>

Assigning HTML contents to PHP variables

Hard to explain, how to assign HTML contents to PHP variables. The HTML contents are not within the PHP script.as
<?php $a_div = ?><div>Contents goes <b>here</b></div>
Try with ob_get_contents: ob_get_contents() or ob_get_clean()
<?php ob_start(); ?>
<div>Contents goes <b>here</b></div>
<?php $contents = ob_get_contents(); ?>
If its raw HTML, with no PHP in it, stored in separate file it would be just
$a_div = file_get_contents('email.html');
If it's PHP file with HTML mixed with PHP then include it using output buffering like in Robik's answer.
ob_start();
include 'mail.template.php';
$a_div = ob_get_contents();
But if it's relatively small, I'd use HEREDOC
$a_div = <<<HERE
<div>Contents goes <b>here</b></div>
HERE;
If needed (normally I'm using html templates, replacing placeholders by php) ...
there are two ways:
$testVar = <<<EndOfHtml
<div style="border: 48px solid red">test</div>
EndOfHtml;
print $testVar
the easier way, not too elegant - wrap html code in single quotes. so you don't have to escape any double quote:
$test_var = '<div style="border: 48px solid red">test</div>'
helpful? :-)
Are you looking for token-terminated Block-Strings?
http://www.phpf1.com/tutorial/php-heredoc-syntax.html
Best I can do. Unfortunately your question is a little vague.
It's a bit late, and it's not exactly what was asked, but if you're looking to render the page out of order (such as with templating), one could also wrap that section with a function and call it to render it in the desired location.
<?php function RenderFooter() { ?>
<footer>some footer</footer>
<?php } ?>
<h1>Some header</h1>
<main>Some content</main>
<?= RenderFooter(); ?>
you can use DOMDocument To parse html file and get contents you need.

HTML into PHP Variable (HTML outside PHP code)

I am new to php and wondering if I can have something like this:
<?php
...
magicFunctionStart();
?>
<html>
<head>...</head>
<body>...</body>
</html>
<?php
$variable = magicFunctionEnd();
...
?>
What I have to use right now is
<?php
...
$variable = "<html><head>...</head><body>...</body></html>"
?>
Which is annoying and not readable.
Have you tried "output buffering"?
<?php
...
ob_start();
?>
<html>
<head>...</head>
<body>...<?php echo $another_variable ?></body>
</html>
<?php
$variable = ob_get_clean();
...
?>
I think you want heredoc syntax.
For example:
$var = <<<HTML
<html>
<head>
random crap here
</html>
HTML;
I'm not really sure about what you are trying to accomplish, but I think something like the heredoc syntax might be useful for you:
<?
$variable = <<< MYSTRING
<html>
<head>...</head>
<body>...</body>
</html>
MYSTRING;
However if you are trying to make HTML templates I would highly recommend you to get a real templating engine, like Smarty, Dwoo or Savant.
Ok what you want to do is possible in a fashion.
You cannot simply assign a block of HTML to a php variable or do so with a function. However there is a number of ways to get the result you wish.
Investigate the use of a templating engine (I suggest you do this as it is worth while anyway). I use smarty, but there are many others
The second is to use an output buffer.
One of the problems you have is that any HTML you have in your page is immediately sent to the client which means it cant be used as a variable in php. However if you use the functions
ob_start and ob_end_fush you can achive what you want.
eg
<?php
somesetupcode();
ob_start(); ?>
<html>
<body>
html text
</body>
</html>
<?php
//This will assign everything that has been output since call to ob_start to your variable.
$myHTML = ob_get_contents() ;
ob_end_flush();
?>
Hope this helps you can read up on output buffers in php docs.
I always recommend to AVOID buffer functions (like ob_start ,or etc) whenever you have an alternative (because sometimes they might conflict with parts in same system).
I use:
function Show_My_Html()
{ ?>
<html>
<head></head>
<body>
...
</body>
</html>
<?php
}
...
//then you can output anywhere
Show_My_Html();
$html_content = '
<p class="yourcssclass">Your HTML Code inside apostraphes</p>
';
echo $html_content;
Its REALLY CRAZY but be aware that if you do it :
<?php echo ""; ?>
You will get it:
<html><head></head><body></body></html>
Keep calm, its only php trying turn you crazy.

Categories