PHP Concatenate file output to string - php

I am rewriting a piece of a program to change it from using "echos" throughout the script to create a large output variable using Heredocs instead, which outputs at the end of the file.
A piece of the script includes another PHP file that directly outputs HTML and has php logic within the HTML it outputs. This file is used by other pieces of the overall program that are not yet being rewritten (due to time constraints).
Is it possible to append the output of another file to an $output variable? I've tried doing this, but it it doesn't work for string appending:
$output .= include 'foo.php';
$output .= file_get_contents('foo.php');
The file_get_contents wrote all the PHP logic directly in HTML, as I suspected it would and the straight 'include' echo'd the HTML as I also expected.
Is there a method to get the output buffer of the file and append to a string?
EDIT: Nevermind the question, I completely forgot about OB_Buffering. Added an answer with my solution, no need to answer this one

I feel stupid. I found the answer 5 minutes after posting, I completely forgot about ob_buffering:
ob_start();
include('./foo.php');
$output .= ob_get_contents();
ob_end_clean();

Related

HTML string to PHP variable without loosing IDE syntax highlighting

I want to add a very large HTML string to a PHP variable. When i do something like $html = "<div>info</div>"; the string will go gray and the normal highlighting no longer works. I want to use some PHP to build the HTML, but most of it will be coded directly into the file. I can't echo the HTML it needs to be in a variable as it gets passed to a function.
Is there another way other than $html = ""; to assign data to a variable that will allow me to keep syntax highlighting. My thoughts would be some sort of syntax that will allow me to close the PHP tag, but won't output the content, but rather saves that output to a variable.
?$html>
<div>content</div>
<?php
I understand this is impossible as the server will not read any lines outside of the PHP tags, but it's just an example to get across what I'm trying to do.
Edit
I have also thought of using
$html = file_get_contents(site.com/file.php);
This would be wasteful as it creates another HTTP request to a PHP page. the page needs to be PHP in order to dynamically build some of the HTML
You can use output buffering with ob_start and ob_get_clean for that:
<?php
ob_start();
?>
<html>
... all your html comes here
</html>
<?php
$html = ob_get_clean();
?>
At the end of the above code, nothing will have been output to the browser, but your $html variable will have the content.

php Format arrays and objects etc output in text logfile

Should be a pretty obvious answer, but I have spent several hours looking at existing similar questions and none are working for me
My code generates logfiles for (manual) debugging etc
If I use print_r($array,TRUE) to capture the output from an array as a string and then echo with <pre> tags to display that on screen, it's really easy to view and understand what's going on.
However, when I write the same info to the logfile, fwrite doesn't preserve the line break and indentation formatting so there is a splurge of info that takes significant amounts of time to make sense of, esp larger arrays and objects.
I have tried using output buffer
$string=print_r($array,TRUE);
ob_start();
echo "<pre>$string</pre>";
$outputBuffer = ob_get_contents();
ob_end_clean();
fwrite($handle,$outputBuffer);
However, all that's now happening is that I see the <pre> tags added into the basic, non-layout output
e.g.
<pre>DOING QUERY: SELECT * FROM event_triggers WHERE DateTime<='2015-09-16 13:04:30'</pre><pre>Completed checking for event triggers</pre>
You can't just add HTML tags to a document, open it in an editor expect HTML tags to be rendered correctly.
You either have to setup your log file as a HTML file (doesn't neccessarily have to be valid, so just add .html to the file name and open it in the browser) or use var_dump to echo out the variables.
Rename file to .html extension and just open with a browser. Browser will detect it with line break html document. <pre></pre> will output like <p></p> in the browser.

How to print code generated by some function in php?

I have a code in my CMS that prints content:<?php print $content ?>
I would like to output the actual php and html code behind $content, ideally in the browser. What I mean here is not the result in the browser, but the actual code behind it.Is it possible at all?
EDIT: Just to explain further: I need to print the source code of $content. Basically this variable produce some html and php content. I would like to see the code it produces, change it and replace $content with my custom code. Ideally the source code should be printed in the browser, is there anny php function that does it?
First off install the Devel Module, it has a wonderful function called dpm() which will print the contents of any variable to the Drupal messages area.
Then you need to go into your theme's template.php file and implement hook_preprocess_page():
function mytheme_preprocess_page(&$vars) {
dpm($vars['content']);
}
That will print out the $content array before it's rendered into a string. In the same preprocess function you can also change $vars['content'] as you see fit, and the changes will be reflected in $content in page.tpl.php.
Hope that helps
What do you mean by 'the code'? I think what you want to do is not possible, unless you make some kind of quine it's not possible to output the actual php code of a php file when you run it.
If $content is something like:
$content = 3 + 4 + 5;
echo $content; will output 12 yes? But I'm taking it you want to output 3 + 4 + 5 or something along those lines. The thing is, PHP (although it doesn't feel like it) is compiled. In this trivial example, 3 + 4 + 5 is stored exactly nowhere in your compiled program, it is stored as 12 (since it's static). More complex lines of code will be stored as pointers, values etc., all in nicely obfuscated machine code. Getting back to the 3 + 4 + 5 requires reading the input file and outputting the relevant line, which is difficult (think about what happens if you add or remove some lines, or how your running program knows where in the source file it is, or even if it's in the right source file).
tl;dr: this is not possible.
Well, if you just want to see html source for $content, you should simply use htmlspecialchars :
echo htmlspecialchars($content);
http://php.net/htmlspecialchars
or http://php.net/htmlentities

var reads HTML code as STRING

Very simply, i want to make a variable reads the html code as string ,, i mean dont execute it (run it) .
the problem with the code is : i have a html file , and i want to get the content of it , and make some preg_replace for it (run a function on the html code), the problem is i cant use preg_replace, or any another function because the html code is executed by php (php reads the html code)..
i wish you understand me, i want something like highlight_string, but it save the html code in the variable.
Thank you.
you're probably trying to include or require the HTML code.
which is incorrect since it is evaluated as part of the source.
instead, use a function such as file_get_contents() to read the file into a string.
Use file_get_contents() as #David Chan suggested and then pass the result through htmlentities()... it converts the characters to HTML entities (i.e., < to <).
$getTheContent = file_get_contents($filepath);
echo htmlentities($getTheContent);
It should return the code, not executed.

Save an include's output to a string? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Storing echoed strings in a variable in PHP
Suppose I have
<?php include "print-stuff.php"; ?>
print-stuff.php contains PHP/HTML template, which means that when it is included, HTML gets printed. Is there any way to capture that HTML as a string, so that I may save it to use elsewhere?
Moving the include statement elsewhere is not an option, because print-stuff.php also performs logic (creates/modifies variables) that the surrounding code depends on. I simply want to move the file's output, while leaving its logic as is.
You can Output Buffer it to make sure the HTML isn't shown and is instead put into a variable. (PHP will still run, but HTML output will be contained in the variable)
ob_start();
include "print-stuff.php";
$contents = ob_get_contents();
ob_end_clean();
....
Honestly I came on here and went ah-ha, I know the answer to this!!! Then I looked down and saw other people got to it before I did.
But, for the heck of it, I do like this:
ob_start();
include 'something.php';
$output = ob_get_contents();
ob_end_clean();
Have a look at output buffering.
http://us2.php.net/manual/en/function.ob-start.php
You can do that if you print in a buffer instead of stdout.
ob_start();
include 'print-stuff.php';
$printedHTML = ob_get_clean();
$fileStr = file_get_contents('/path/not/url/to/script.php');

Categories