Possible to treat html that is directly outputted as a php string - php

I would simply like to know if something similar to this is possible in php somehow:
<?php
$myhtmlstring = "
?>
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
";
?>
The reason for this is I would like to be able to write the html in this nice looking format but have php trim the white space after the fact.

You can use heredoc.

You can use the alternative heredoc syntax:
$myhtmlstring = <<<EOT
<table>...</table>
EOT;
Or you can use output buffering:
<?php
ob_start();
?>
<table>...</table>
<?php
$myhtmlstring = ob_get_clean();
?>

Yes
<?php
$myhtmlstring = '
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
';
// Do what you want with the HTML in a PHP variable
// Echo the HTML from the PHP variable to make the webpage
echo $myhtmlstring;
?>

I usually use the buffer functions, like so:
<?php
$whatever = "Hey man";
// This starts the buffer, so output will no longer be written.
ob_start();
?>
<html>
<head>
<title><?php echo $whatever ?></title>
</head>
<body>
<h1><?php echo $whatever ?></h1>
<p>I like this in part because you can use variables.</p>
</body>
</html>
<?php
// Here's the magic part!
$myhtmlstring = ob_get_clean();
?>
For more information about the buffer functions, look up ob_start() on
php.net.

do you mean so?
<?php
$string = '<table border="1">
<tr>
<td> test </td>
</tr>
</table>';
echo $string;
?>

Related

HI~ I am wondering if there is a method to require a PHP file in an echo

i am wondering if there is a method to require another php file in an echo like this:
<?php
echo "
<label>Post Notifications</label>
<div>
<?php require_once('libraries/notifications/post_notifications.php'); ?>
</div>";
?>
I know the <?php require_once('libraries/notifications/post_notifications.php'); ?> isn't correct but just hope you can get the idea. Thank you!
Use ob_get_contents. Everyting between this tags is stored in variable end cleaned.
ob_start();
include('libraries/notifications/post_notifications.php');
$output = ob_get_contents();
ob_end_clean();
So this Looks output like:
<?php
echo "
<label>Post Notifications</label>
<div>
{$output}
</div>
";
?>

put string in variable using <? ?>

I think it is sometimes easier to use php tags instead of echo for example
<?
if()
echo "<img src='' onclick='alert(\"hello\")'/>";
?>
instead of that I code like this
<?
if(){
?>
<img src='' onclick='alert("hello")'/>
<?}
?>
We got rid of backslashing. But what about strings I want something like this:
<?
$str="?>
<img src='' onclick='alert("hello")'/>
<?";
?>
You should use the PHP heredoc syntax:
<?php
$str = <<<IMGTAG
<img src="" onclick="alert('hello')"/>
IMGTAG;
echo $str;
?>
Enjoy your code.
There is an alternative Syntax specifically for this kind of formation:
<?php if (x): ?>
<div>...</div>
<?php endif; ?>
Also there are short tags:
<?= "hello world" ?>
This directly prints a string and is equal to:
<?php echo "hello world" ?>
For string assignment you can do what Magicianred sugested. You could also do it with output buffering:
<?php ob_start(); ?>
<div>test</div>
<?php
$str = ob_get_contents();
ob_end_clean();
echo $str;
?>
Though output buffering shouldn't be abused for this. Heredoc syntax is the best solution here.

PHP - Placing html in variable on out of php tag

I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>

PHP: count the words in a DIV

I say PHP, because I have this snippet to count the words with PHP, maybe it's better with jQuery?
$words = str_word_count(strip_tags($myString));
I have a PHP page with static HTML mixed with some PHP variables like so:
<?php
$foo = "hello";
?>
<html>
<body>
<div>total words: <?= $words ?></div>
<div class="to_count">
<?= $foo ?> <b>big</b> <i>world</i>, how <span>are</span> we today?
</div>
</body>
</html>
I tried looking into PHP's output buffering and slipped an ob_start() and $buffer = ob_get_clean(); around the .to_count DIV, but I can't seem to use the $buffer in the top section on the PHP page to count the words.
Any help to set me on the way is appreciated, cheers.
With jQuery and regex:
var wordCount = $.trim($(".to_count").text()).split(/\s+/g).length;
you can't use buffer before it is declared. If you do it will default to a value that isn't useful. I recommend counting the words before inserting them into the HTML and setting a variable with the count.
I recommend building the content of the .to_count div before actually rendering it. Something like this:
<?php
$foo = "hello";
$content = "$foo <b>big</b> <i>world</i>, how <span>are</span> we today?";
$words = str_word_count(strip_tags($content));
?>
<html>
<body>
<div>total words: <?= $words ?></div>
<div class="to_count"><?= $content ?></div>
</body>
</html>
You could use output buffering to generate it. Which I think is tider than generating HTML in php.
<?php
ob_start();
$foo = "hello";
?>
<?php echo $foo ?> <b>big</b> <i>world</i>, how <span>are</span> we today?
<?php
$myString = ob_get_contents();
ob_end_clean();
$words = str_word_count(strip_tags($myString));
?>
<html>
<body>
<div>total words: <?php echo $words ?></div>
<div class="to_count">
<?php echo $myString ?>
</div>
</body>
</html>

How to create a custom template system in PHP

I want to use a custom template system in my php application,
What I want is I want to keep away my php codes from design, I would like to use a tpl file for designs and a php file for php codes
I dont want to use any ready maid scripts. Can any one point out some links link or useful info how to build a php templating system to achieve this
Thank you
The way I do it is to create a template file(.tpl if you wish) and insert markers which will be replaced with str_replace in PHP. The code will look something like this:
For template.tpl file
<body>
<b>Something: </b> <!-- marker -->
</body>
For the PHP
$template = file_get_contents('template.tpl');
$some_data = 'Some Text'; //could be anything as long as the data is in a variable
$template = str_replace('<!-- marker -->', $some_data, $template);
echo $template;
That's it in a nutshell but it can get a lot more complex. The marker can be anything as long as it's unique.
I want to keep away my php codes from design, I would like to use a tpl file for designs
...and mix your tpl codes with "design"!
what's the difference then? :)
PHP itself is efficient templating system.
And nowadays most developers agreed that dividing your PHP code to business logic part and display logic part is most preferable way.
It can be very limited subset of PHP of course. You will need an output operator (<?=$var?>) one, a condition <? if(): ?>...<? endif ?>, a loop <? foreach(): ?>...<? endforeach ?> and include.
An example of such a template:
<table>
<? foreach ($data as $row): ?>
<tr>
<td><b><?=$row['name'] ?></td>
<td><?=$row['date'] ?></td>
</tr>
<tr>
<td colspan=2><?=$row['body'] ?></td>
</tr>
<? if ($row['answer']): ?>
<tr>
<td colspan=2 valign="top">
<table>
<tr>
<td valign="top"><b>Answer: </b></td>
<td><?=$row['answer'] ?></td>
</tr>
</table>
</td>
</tr>
<? endif ?>
<? if($admin): ?>
<tr>
<td colspan=2>
<? if($row['del']): ?>
show
<? else: ?>
hide
<? endif ?>
edit
</td>
</tr>
<? endif ?>
<? endforeach ?>
</table>

Categories