Really big HTML and Javascript to PHP variable - php

I have got a Html and Javascript code, that contains about 1000 lines and I need to put it to php variable.
Sure I was thinking about the EOT method, But there is one problem with it, if there is word function like in javascript is, it will take it like php function, and this will cause errors.
Any other Idea how to do it?
I have already tried other forums, but they can't help me, so I hope they can help me on the best.

Maybe use output buffering...
<?php
ob_start();
?>
<b>
<u>
<font color="#FF0000">
<blink>
<marquee>
1000
LINES
OF
HTML
AND
JAVASCRIPT!
</marquee>
</blink>
</font>
</u>
</b>
<?php
$content = ob_get_contents();
ob_clean();
?>
Then your HTML and JavaScript will be in the $content variable.

You could read directly from an HTML file on disk, using file_get_contents().

You can use the EOF method.
There's no problem with reserved words in that case. (As far as I know)
EDIT:
$output .= <<<HTML
function bla()
{
//Something
}
HTML;
Won't be treated as a php function.

Try this;
class Temp
{
public function html($path)
{
ob_start()
require(path); // or file_get_contents(<URI>);
$html = ob_get_clean ();
return $html
}
}
$temp = new Temp();
$htmlData = $temp->html('somepath/somefile.php')
echo $htmlData;

Related

Using html code in php function or no?

i would to know what is good practice for writing code to put all HTML code inside PHP function and in my front index.php file just call function to show code.
class.php:
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
echo '<ul class="centreList">';
while($nem->fetch())
{
echo '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
echo '</ul>';
}
index.php:
<?php $connection->test(); ?>
This work fine, but I would like to know is this proper way or is not a good practice to use html code inside PHP functions?
It's ok to build HTML within PHP, but I would not echo to the screen directly from within the function. Instead, return the built HTML string.
$html = '<ul class="centreList">';
while($nem->fetch())
{
$html .= '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
$html .='</ul>';
return $html
The function should not be responsible for pushing content to the browser because it really limits what you can do with your code. What if you wanted to further process the HTML? What if you run into a condition later in the code and decided to abort? What if you wanted to set some response headers later? Some content would already be gone so none of these things would be possible without clever workarounds.
In general you want to separate your responsibilities: I would even break things down further:
one piece of code is in charge of retrieving info from the DB and returning
Another piece is in charge of building the HTML string
A third piece is in charge of displaying the HTML (probably your index.php)
New index.php
<?= $connection->test(); ?>
Do not use echo to print the html directly, wrap the html within while loop surrounded by php tags
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
return $nem;
}
<ul class="centreList">
<?php $res = test()->fetch();
while( $res->fetch() ) { ?>
<li> <?php echo $id ?> Download </li>;
<?php } ?>
</ul>

Is it possible to create a function in PHP called "echo"? [duplicate]

I recently looked at my source code and it was a real mess.
my php source:
echo '<h1>Rar<h1>';
echo '<span>Rar</span>';
echo '<p>Rar</p>';
and when I view the browser source for the page rendered:
<h1>Rar</h1><span>Rar</span><p>Rar</p>
is there a way for me to override echo so that every output would end with a newline, something like
function echo($string)
{
echo $string . "\r\n";
}
echo is not a function, but a language statement. It cannot be redefined. If you are looking to prettify your output markup, have look at Tidy.
What you could do, is use your IDE's search/replace method and replace all echo statements with echo PHP_EOL,. This would append the OS specific newline char(s) before any output. Note the comma after PHP_EOL as it is important.
You can output several values with echo like this:
echo 'one', $foo, PHP_EOL,
'two', $bar, PHP_EOL;
so there is no need to write echo on each line.
However, I agree with anyone who suggested using a more dedicated approach to separate content and layout e.g. using template views or HereDoc.
In additon, there is very little gain in having pretty markup. If you are using tools like Firebug to inspect the HTML, you will have properly formatted markup regardless of the mess the markup really is. Moreover, on sites with a lot of visitors, you'll often find the markup minified, which is the opposite of what you are trying to do, simply because all these newlines and tabs add to the weight of the page, which leads to slower page loads and increased traffic cost.
You have various possibilities to output HTML.
You can use the heredoc syntax:
$html = <<<EOF
<h1>Rar<h1>
<span>Rar</span>
<p>Rar</p>
EOF
echo $hml;
Or (what is way better in my opinion), separate HTML from PHP. E.g. put all the PHP logic in the top of the file and the HTML after the PHP block:
<?php
// all your PHP goes here
$foo = 'bar'
?>
<!-- HTML comes here -->
<html>
<body>
<div>Hello <?php echo $foo; ?> </div>
</body>
</html>
Variables can be printed as shown above. But these variables don't contain HTML.
When you have to output HTML based on a condition, you can use the alternative syntax for control statements:
<?php if($some_condition): ?>
<h1>Rar<h1>
<span>Rar</span>
<p>Rar</p>
<?php endif ?>
This way it is also easier to debug your HTML as it is not only a PHP string.
You can set up and output buffer and then run the buffer through htmltidy. The tidy extension even has a specific function for the purpose. Just call this before you start outputting your html:
ob_start('ob_tidyhandler');
Although this solution does not override echo, you can get something close to echo with a newline. Add:
function e() {
return o::singleton();
}
class o {
private static $instance;
public static function singleton()
{
if (!isset(self::$instance)) {
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
public function __set($prop, $txt) {
echo $txt . PHP_EOL;
}
}
to your file, and then you can use:
e()->o = "Line which ends in newline";
instead of echo.
Another solution would be to separate your code from your layouts by using a proper templating engine.
You can indirectly overload echo() by using the __toString() magic method like so:
<?php
class CleanOutput
{
public $content;
public function __construct($c) {
$this->content= $c;
}
public function __toString() {
return $this->content . '\r\n';
}
}
$text= new CleanOutput('Hello world!');
echo $text;
?>
The above would output "Hello world!" with a newline and carriage return appended at the end. There's ways to further encapsulate this, but they are outside the scope of my answer.
Edit:
As was noted, the above solution is slow/clumsy. Here's a more elegant solution using output buffering:
<?
function clean_up($foo) {
return $foo . '\r\n';
}
ob_start('clean_up');
ob_implicit_flush(true);
echo "Hello world!";
?>
This is faster and cleaner (although it technically doesn't 'override' echo).

Capturing HTML to a string in PHP

Okay, so maybr I'm going about doing this entirely wrong, I probably am. But I would like to be able to take the HTML between a ... like so:
$str = ?>
... some HTML goes here ...
<?php ;
Am I completely off my rocker to think I can do this? I couldn't think of a way to put it into words so I could search it on Google, which is why I'm here...
You can use output buffering:
ob_start();
?>
... some HTML goes here ...
<?php
echo 'php outputs are captured too';
$str = ob_get_contents();
ob_end_clean();
Alternatively, if it's just a little bit of HTML (and no php code within), just write it down with one of the string formats like heredoc or nowdoc:
$str = <<<'NOWDOC'
... some HTML goes here
NOWDOC;
Look into heredocs and nowdocs. A heredoc looks like:
$str = <<<HTML
<div>This is some text!</div>
HTML;
// We're back in PHP.
echo $str;
If you specifically want to work with HTML, look into XHP.
Just wanted to add to phihag's answer.
It is possible to capture HTML with a function as well, including with anonymous functions:
<?php $bob = function() { ?>
... some HTML here...
<?php }; ?>
and later output $bob:
<?php $bob(); ?>
or capture the output of $bob somewhere else with output buffering:
ob_start();
$bob();
$str = ob_get_contents();
ob_end_clean();
PHP has a multiline, specially delimited string for such situations.
This talks about it a little.

Can I override the PHP built-in function echo()?

I recently looked at my source code and it was a real mess.
my php source:
echo '<h1>Rar<h1>';
echo '<span>Rar</span>';
echo '<p>Rar</p>';
and when I view the browser source for the page rendered:
<h1>Rar</h1><span>Rar</span><p>Rar</p>
is there a way for me to override echo so that every output would end with a newline, something like
function echo($string)
{
echo $string . "\r\n";
}
echo is not a function, but a language statement. It cannot be redefined. If you are looking to prettify your output markup, have look at Tidy.
What you could do, is use your IDE's search/replace method and replace all echo statements with echo PHP_EOL,. This would append the OS specific newline char(s) before any output. Note the comma after PHP_EOL as it is important.
You can output several values with echo like this:
echo 'one', $foo, PHP_EOL,
'two', $bar, PHP_EOL;
so there is no need to write echo on each line.
However, I agree with anyone who suggested using a more dedicated approach to separate content and layout e.g. using template views or HereDoc.
In additon, there is very little gain in having pretty markup. If you are using tools like Firebug to inspect the HTML, you will have properly formatted markup regardless of the mess the markup really is. Moreover, on sites with a lot of visitors, you'll often find the markup minified, which is the opposite of what you are trying to do, simply because all these newlines and tabs add to the weight of the page, which leads to slower page loads and increased traffic cost.
You have various possibilities to output HTML.
You can use the heredoc syntax:
$html = <<<EOF
<h1>Rar<h1>
<span>Rar</span>
<p>Rar</p>
EOF
echo $hml;
Or (what is way better in my opinion), separate HTML from PHP. E.g. put all the PHP logic in the top of the file and the HTML after the PHP block:
<?php
// all your PHP goes here
$foo = 'bar'
?>
<!-- HTML comes here -->
<html>
<body>
<div>Hello <?php echo $foo; ?> </div>
</body>
</html>
Variables can be printed as shown above. But these variables don't contain HTML.
When you have to output HTML based on a condition, you can use the alternative syntax for control statements:
<?php if($some_condition): ?>
<h1>Rar<h1>
<span>Rar</span>
<p>Rar</p>
<?php endif ?>
This way it is also easier to debug your HTML as it is not only a PHP string.
You can set up and output buffer and then run the buffer through htmltidy. The tidy extension even has a specific function for the purpose. Just call this before you start outputting your html:
ob_start('ob_tidyhandler');
Although this solution does not override echo, you can get something close to echo with a newline. Add:
function e() {
return o::singleton();
}
class o {
private static $instance;
public static function singleton()
{
if (!isset(self::$instance)) {
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
public function __set($prop, $txt) {
echo $txt . PHP_EOL;
}
}
to your file, and then you can use:
e()->o = "Line which ends in newline";
instead of echo.
Another solution would be to separate your code from your layouts by using a proper templating engine.
You can indirectly overload echo() by using the __toString() magic method like so:
<?php
class CleanOutput
{
public $content;
public function __construct($c) {
$this->content= $c;
}
public function __toString() {
return $this->content . '\r\n';
}
}
$text= new CleanOutput('Hello world!');
echo $text;
?>
The above would output "Hello world!" with a newline and carriage return appended at the end. There's ways to further encapsulate this, but they are outside the scope of my answer.
Edit:
As was noted, the above solution is slow/clumsy. Here's a more elegant solution using output buffering:
<?
function clean_up($foo) {
return $foo . '\r\n';
}
ob_start('clean_up');
ob_implicit_flush(true);
echo "Hello world!";
?>
This is faster and cleaner (although it technically doesn't 'override' echo).

PHP: Breaking out of PHP for HTML but return as value instead of print on page?

I am generating a lot of HTML code via PHP, but I need to store it in a variable, not display it immediately. But I want to be able to break out of PHP so my code isnt a giant string.
for example (but actual code will be much larger):
<?php
$content = '<div>
<span>text</span>
link
</div>';
?>
I want to do something like this:
<?php
$content =
?>
<div>
<span>text</span>
link
</div>
<?php
;
?>
But I know this will not return the html as a value it will just print it to the document.
But is there a way to do this tho?
Thanks!
You can use output buffering:
<?php
ob_start();
?>
<div>
<span>text</span>
link
</div>
<?php
$content = ob_get_clean();
?>
Or a slightly different method is HEREDOC syntax:
<?php
$content = <<<EOT
<div>
<span>text</span>
link
</div>
EOT;
?>
Try this:
<?php
$var = <<<EOD
my long string
EOD;
echo $var;
?>
(edit done, but one has edit faster than me :))
In my opinion, you should look into a template engine such as Smarty. It can help you take some of the ugliness out of hardcoding HTML into the PHP file, and can help make the code more manageable.
You could store the html in an html file and read that file into a variable.
While still technically a string, you might find the documentation on PHP's heredoc to be an entertaining read:
heredoc syntax
$content = <<<EOT
<div>
<span>text</span>
link
</div>
EOT;
Use heredoc syntax.

Categories