Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning HTML contents to PHP variables

Tags:

php

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>
like image 738
Shahid Karimi Avatar asked Apr 06 '11 07:04

Shahid Karimi


2 Answers

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(); ?>
like image 130
Robik Avatar answered Sep 20 '22 02:09

Robik


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;
like image 43
Your Common Sense Avatar answered Sep 19 '22 02:09

Your Common Sense