import content of php file into a php variable - php

I have an external php file like this:
<!DOCTYPE>
<head>...
...
<body>
<p><?php echo $content;?></p>
..
AND in my code:
$content = 'sample text';
$body = include("layout/mailtemplate.php");
You can see it has php and html code (and pass $content outside of file to included file)
Is there any way to store content of this file to a php variable?
(here content of $body is "1"!)
Also I test
$body = file_get_contents('layout/mailtemplate.php');
It works, but I could not pass $content to file.
(I know I could pass it via GET) but I have a lot of variables. Is there a simpler way?

Yes, you can. You need to use output buffering for that:
ob_start();
$content = 'sample text';
include("inc.php");
$body = ob_get_contents();
ob_end_clean();
var_dump($body); // string(11) "sample text"

Related

Is it possible to have access to all the strings written before, on a page, with PHP?

Does PHP give access to all the strings that have been outputted to a page?
<html>
<body>
Link
<?php
echo 'hello world';
echo 'something else';
$s = get_all_output(); // does this exist ?
Of course I could replace every instance of echo 'some text'; by an initial $s = ''; and then $s .= 'some text';.
But without this trick, how to get the current page as a string?
You could make use of the output buffering of PHP (cf. https://www.php.net/manual/en/function.ob-get-contents.php).
<?php
ob_start(); // turn on buffering
echo "Hello ";
$out = ob_get_contents();
ob_end_clean(); // end buffering and clear buffer without "displaying it".
// process $out which contains "Hello "
echo "$out";
You must insert:
ob_start();
at the beginig of your php code and then
$s = ob_get_contents();
at the point where you want to get the content of the page.

include html code inside php with determine variable

I have a PHP code which sends an email with HTML template.
HTML template has some PHP variable like Name, Email, etc.
<strong>Name: {name} </strong>
because email template has more code, I include it in my PHP file
$htmlInvoice = file_get_contents($_SERVER["DOCUMENT_ROOT"] .'/mail/invoice.html');
$name= $user['name']);
$msg = $htmlInvoice;
But when an email sent, it can not read variables inside the
HTML file. and for example echo $name like a text
You can use strtr for this. For e.g:
<?php
$a = 'Hi this is {name}. I am writing {language}';
echo strtr($a, ['{name}' => 'Abhishek', '{language}' => 'php']);
Strtr
You need to replace the {name} string for your desired output.
$htmlInvoice = file_get_contents($_SERVER["DOCUMENT_ROOT"] .'/mail/invoice.html');
$name= $user['name']);
$msg = str_replace('{name}',$name, $htmlInvoice);
You could change /mail/invoice.html to include PHP commands.
<strong><? echo $user['name'] ?></strong>
Then
$htmlInvoice = file_get_contents($_SERVER["DOCUMENT_ROOT"] .'/mail/invoice.html');
$msg = _readphp_eval($htmlInvoice);
function _readphp_eval($code) {
ob_start();
print eval('?>'. $code);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
I don't know who to attribute _readphp_eval($code) function to ... It is floating around the internet, and works very nicely for this purpose.
It would require that $user['name'] be defined/included in the invoice.html file.

How to check if text is present on a webpage?

how to check if text is present on a webpage using php and if true to execute some code?
My idea is to show some relevant products on the confirmation page after completing an order - if the name of the product is present on the page, then load some products. But I can't make the check for present text.
Case 1 if you prepare your page in a variable then echo it at the end of the script like
$response = "<html><body>";
$response .= "<div>contents text_to_find</div>";
$response .= "</body></html>";
echo $response;
then you can merely search the string with any string search function
if(strpos($response,"text_to_find") !==false){
//the page has the text , do what you want
}
Case 2 if you don't prepare the page in a string . and you just echo the contents and output the contents outside the <?php ?> tags like
<?php
//php stuff
?>
<HTML>
<body>
<?php
echo "<div>contents text_to_find</div>"
?>
</body>
</HTML>
Then you have no way to catch the text you want unless you use output buffering
Case 3 if you use output buffering - which I suggest - like
<?php
ob_start();
//php stuff
?>
<HTML>
<body>
<?php
echo "<div>contents text_to_find</div>"
?>
</body>
</HTML>
then you can search the output anytime you want
$response = ob_get_contents()
if(strpos($response,"text_to_find") !==false){
//the page has the text , do what you want
}
You may need to buffer your Output like so...
<?php
ob_start();
// ALL YOUR CODE HERE...
$output = ob_get_clean();
// CHECK FOR THE TEXT WITHIN THE $output.
if(stristr($output, $text)){
// LOGIC TO SHOW PRODUCTS WITH $text IN IT...
}
// FINAL RENDER:
echo $output;
Fastest solution is using php DOM parser:
$html = file_get_contents('http://domain.com/etc-etc-etc');
$dom = new DOMDocument;
$dom->loadHTML($html);
$divs = $dom->getElementsByTagName('div');
$txt = '';
foreach ($divs as $div) {
$txt .= $div->textContent;
}
This way, variable $txt would hold the text content of a given webpage, as long as it is enclosed around div tags, as usually. Good luck!

How to use file_put_contents() for part of the php file

I'm using PHP and html in order to develop a simple mechanism that creates reports and send them by Email.
I use the function file_put_contents() and the function ob_get_contents() as a parameter in order to create an html file which I use to send by the mail.
I realized that if I use ob_get_contents() without using ob_start() it simply takes all the file and put it to an html file. This is not good for me since I want only parts of the file to be in the generated html. To be more clear my code looks something like this:
<html and php code I want to include in my html file>
.
.
<html and php code I don't want to include in my html file>
.
.
<html and php code I want to include in my html file>
.
.
<html and php code I don't want to include in my html file>
.
.
.
file_put_contents('report.html', ob_get_contents());
$message = file_get_contents('report.html');
mail($to, $subject, $message, $Headers);
So how do I choose only the parts I want to be included in report.html?
Thank you very much!
You are doing it unnecessarily difficult, you don't need external files to generate your report. Take a look at this:
<?php
$report = '';
// ...
// Code not included in your report
// ...
ob_start();
// ...
// HTML and PHP code you want in your report
// ...
$report .= ob_get_clean();
// ...
// Code not included in your report
// ...
ob_start();
// ...
// HTML and PHP code you want in your report
// ...
$report .= ob_get_clean();
// Mail it
mail($to, $subject, $report, $headers);
?>
EDIT: Regarding to OP's comment.
What you need is ob_get_flush() instead of ob_get_clean(). Both return the buffer contents as a string, but the first one dumps it to the script output while the second one empties the buffer instead.
May or May not Help
I always handle this the same way I load pages when using vanila PHP, with a snippet! The following is one I've kept around forever and used. It has 2 possible primary functions. One is to load a view (html page), the other is to get a page of html as a string for such things as inclusion in an email body.
For example:
// will load a page into the clients browser
// note the page location would indicate it will get the file "index.html" from "$_SERVER["DOCUMENT_ROOT"] . '/views/'"
loadView('/views/index.html');
// or, as would be more helpful to you
$msgHTML = loadView('/views/index.html', NULL, TRUE);
The TRUE param simply tells the function to only return a string and not echo anything to a client.
The NULL param you see there is for an array of data to be passed through. For instance, say you have an html page with a table you want populated for a database call. You would simply make your call, place your return in an array and then add to page.
$arr = array( 'key' => 'value' );
$msgHTML = loadView('/views/index.html', $arr, TRUE);
// then in the index.html
<div><?= $key; ?></div>
This makes it very easy to build anything HTML you need for an Email.
Snippet
if (!function_exists('loadView')) {
function loadView($file, $data=NULL, $get=FALSE) {
if (!empty($data)) extract($data);
ob_start();
if (is_file($file)) include($file);
$return = ob_get_clean();
if (!$get) echo($return);
return $return;
}
}
Thus you could do something like:
$htmlFirst = loadView('report.html', NULL, TRUE);
$msgFirst = 'Some message string here';
$htmlSecond = loadView('report2.html', NULL, TRUE);
$msgSecond = 'Some message string here';
$body = $htmlFirst . $msgFirst . $htmlSecond . $msgSecond;
mail($to, $subject, $body, $Headers);

php evaluate code before getting file content

I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).
I tried using $html = file_get_content("B590.php");
But then $html will have the content of B90.php as plain text(with php code).
Is there any method where I can get the content of the file after it has been evaluated?
There seems to be many related questions like this one and this one but none seems to have any definite answer.
You can use include() to execute the PHP file and output buffering to capture its output:
ob_start();
include('B590.php');
$content = ob_get_clean();
function get_include_contents($filename){
if(is_file($filename)){
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
$html = get_include_contents("/playbooks/html_pdf/B580.php");
This answer was originally posted on Stackoverflow
If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:
ob_start();
include('B590.php');
$html = ob_get_clean();
Example:
B590.php
<div><?php echo 'Foobar'; ?></div>
current.php
$stuff = 'do stuff here';
echo $stuff;
include('B590.php');
will output:
do stuff here
<div>Foobar</div>
Whereas, if current.php looks like this:
$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;
The output will be:
do stuff here
Some more
<div>Foobar</div>
To store evaluated result into some variable, try this:
ob_start();
include("B590.php");
$html = ob_get_clean();
$filename = 'B590.php';
$content = '';
if (php_check_syntax($filename)) {
ob_start();
include($filename);
$content = ob_get_clean();
ob_end_clean();
}
echo $content;

Categories