Mailing HTML content with PHP - php

I would like to mail the content of a Web page that looks like:
<html>
<body>
<?php
function sendPageContentToEmail($destEmail)
{
ob_start();
$buffer = ob_get_contents();
ob_end_clean();
$subject = 'Subject name';
mail($destEmail, $subject, $buffer);
}
?>
<div style="width:400px; margin:0 auto;">
<p>
Name: <?php print($customerData['customer_name']); ?>
</p>
<p>
....
</p>
</div>
</body>
</html>
<?php
sendPageContentToEmail($customerData['customer_email']);
//erase all temp data
session_destroy();
?>
$buffer is always empty (ob_get_content()) regardless of where the sendPageContentToEmail() is called.
Where should this function be called (assuming that it is the right way to do it)?

What ob_start does is start caching all the output, so, if you output something before calling it, that part won't be cached.
Just at the start, before <html> do a <?php ob_start(); ?>

Related

How to delete all data in div by id after using file_get_contents php php [Using PHP]?

How to delete all data in div by id after using file_get_contents php [Using PHP]?
I want to delete all data inside div id="one"
This is coding on https://www.example.com
<div id="main">
<div id="inner">
<div id="one">
<div>
HELLO
</div>
HELLO
</div>
<div id="two">
TEST
</div>
</div>
</div>
.
<?PHP
$home_page = file_get_contents("https://www.example.com");
echo $home_page;
?>
When finished i want to get data like this
<div id="main">
<div id="inner">
<div id="two">
TEST
</div>
</div>
</div>
How can i do ?
Best regards,
mongkon tiya
Edit: Readed you want an plain PHP solution, sry this is a JS solution.
echo '<script type="text/javascript">',
'var elem = document.getElementById("one"); elem.remove();',
'</script>'
Just call in php via echo after your file_get_contents a script. I write it in plain js, if its not working and ure using an older browser use:
echo '<script type="text/javascript">',
'var elem = document.getElementById("one"); elem.parentNode.removeChild(elem);
'</script>
If you use Jquery u can also just use the function .remove();
In order to accomplish this sort of DOM manipulation using only PHP you need to use output buffering and DOMDocument
<?php
ob_start(); /* tell php to buffer the output */
?>
<!-- typical html page - there MUST be a doctype though!-->
<!--
<!doctype html>
<html>
<head>
<title>Output buffer test</title>
</head>
<body>
<div id='main'>
<div id='inner'>
<div id='one'>
<div>
Hello World
</div>
hello
</div>
<div id='two'>
Goodbye Cruel World
</div>
</div>
</div>
</body>
</html>
-->
<?php
echo file_get_contents('http://www.example.com');
?>
<!-- manipulate the DOM using PHP only -->
<?php
libxml_use_internal_errors( true );
/* read the currently stored buffer and load into DOMDocument */
$buffer=#ob_get_contents();
$dom=new DOMDocument;
$dom->loadHTML( $buffer );
libxml_clear_errors();
/* Find the DOM element you wish to remove */
$div=$dom->getElementById('one');
$div->parentNode->removeChild( $div );
/* Save the current DOM and flush the buffer */
$buffer=$dom->saveHTML();
#ob_end_clean();
echo $buffer;
if( #ob_get_level() > 0 ) {
for( $i=0; $i < ob_get_level(); $i++ ) #ob_flush();
#ob_end_flush();
}
$dom=null;
?>

Return big HTML block with some php without make string

How can i return big html block with some php by using <<<HTML HTML; .
return <<<HTML
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php } ?>
<?php echo $whatever; ?>
HTML;
I can't understand what will work and what will not! how should i use this kind of return <<<HTML HTML; block with some php variable that i need to echo and some function that echo some thing (not return)
You can use 'capture output' for this task. see Output Control Functions
i has some example code that i have just tested. It captures the output of the div tag in $out1 and shows it again later.
This technique is used in many 'templating' libraries and in 'views' in the 'frameworks'.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test of Output control functions</title>
</head>
<body>
<?php ob_start(); // capture the buffer ?>
<div style="border: 4px solid red">
<p>This is a test paragraph</p>
<p>This is test PHP code: <?php echo time(); ?></p>
</div>
<?php $out1 = ob_get_contents(); // end capture ?>
</body>
</html>
<?php echo $out1; // output now or save for later. ?>
<?php var_dump($out1, strlen($out1)); ?>
<?php exit; ?>
Okay, google heredoc syntax for PHP.
but this is how it works (which I think you are trying to do.
$html = <<<HTML
<div>
<h1>$phpVariableTitle</h1>
<div>
{$thisFunctionEchosomthingNotReturn()}
</div>
</div>
HTML;
return $html;
Try that. IMPORTANT! heredoc syntax requires your closing tag be left aligned with no tabs. So make sure there are no spaces or tabs to the left of your heredoc tags, in this example my heredoc tags are called HTML. Also, wrapping your php variables/functions with curly braces is optional but good practice for this method. NO PHP tags in side heredoc block.
Hope that helps.
To make a conditional statement work inside you need to use a function:
class My_Class {
public function myCondition($param) {
if($param === true) {
return '<p>True</p>';
} else {
return '<p>False</p>';
}
}
}
$object =new My_Class();
$html = <<<HTML
<div>
<h1>Conditional Statement</h1>
<div> {$object->myCondition(true)} </div>
</div>
HTML;
something like that should work. But I haven't tested it.
I am unable to understand your question properly may be this may help:
<HTML>
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn();
if($isflag){?>
<span>DO not do this</span>
<?php }//Closing If if it ends here.
echo $whatever; ?>
</HTML>
You cannot write control structures / functions logic inside of HEREDOC syntax.
Alternate way..
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php echo $whatever; }?>

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;
?>

How to generate pdf from php functions & variables in TCPDF

i have functions which takes stylesheet & gets output from other classes using OOP in PHP.
i need to generate the pdf using TCPDFm but i am facing problem how to pass in $html variable, i have read the manual for XHTML+ CSS using that does not explain the PHP variable output.
I am pating my code here, please help.
<?php
// define some HTML content with style
$html = <<<EOF
<?php $frame->HTML_DOCTYPE(); ?>
<html>
<head><?php $frame->HTML_Title($page); $frame->HTML_CSS($page); $frame->HTML_JS($page); ?></head>
<body>
<div class='container_pngfix'>
<?php echo $frame->Print_Top($page); ?>
<div class='container_magazine'>
<div class='magazine_left'>
<?php
/*if(FALSE == empty($_SESSION['wi_id']) && FALSE == empty($_SESSION['wi_type']) && 0 == strcasecmp('bride',$_SESSION['wi_type'])){*/
if ($_REQUEST['magazineId']!="") {
echo $article->Print_Magazine_Issue($_REQUEST['magazineId']);
} else if ($_REQUEST['latestissue']) {
echo $article->Print_Magazine_Issue($article->getLatestMagazineId());
} else {
if (isset($_REQUEST['magazinedate'])) { $magazinedate=$_REQUEST['magazinedate']; } else { $magazinedate=""; }
if ($magazinedate=="") { $magazinedate=date("Y-m")."-01"; }
echo $article->Print_Magazine($magazinedate);
}
/*}else{
echo "<h2>Please Login/Register first to see Magazine details</h2>";
}*/
?>
</div>
<div class='magazine_right'><?php echo $frame->Print_RightSide_Articles($page); ?></div>
<div class='clearfloat'> </div>
<?php echo $frame->get_ContextualWeb($page,"low"); ?>
</div>
</div>
<?php echo $frame->Print_Bottom_Links($page); ?>
<?php $frame->Print_Bottom($page); ?>
</div>
</body>
</html>EOF;
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_061.pdf', 'I');
?>
In this code i am calling multiple functions using objects of those classes.
All html & content of page is generating dynamically. i have problem of passing the PAGE html & PHP code in $html variable.
You can use ob_start() and ob_get_clean() to get any output.
<?php
ob_start();
?>
<!-- HTML and PHP code here -->
<?php
$html = ob_get_clean();
?>
$html will have your data that you can send to TCPDF
example for your situation:
<?php
// your other script
ob_start();
?>
<?php $frame->HTML_DOCTYPE(); ?>
<html>
<head><?php $frame->HTML_Title($page); $frame->HTML_CSS($page); $frame->HTML_JS($page); ?></head>
<body>
<!-- rest of the page -->
</body>
</html>
<?php
$html = ob_get_clean();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_061.pdf', 'I');
?>

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>

Categories