I'm attempting to file_get_contents and output php code as a string without being rendered. The idea is to grab the raw un-rendered file contents so they can be edited in a textarea...
// file "foo.php" I'm needing the contents of
<h1>foo</h1>
<? include 'path/to/another/file' ?>
// php file that's calling the file_get_contents
<?php
echo file_get_contents('foo.php');
?>
The above code is stripping out the php include in foo.php which outputs:
<h1>foo</h1>
Does anyone know how I can get foo.php contents as a raw un-rendered string where output will be?:
<h1>foo</h1>
<? include 'path/to/another/file' ?>
Any help is greatly appreciated!
As far as I know you can't get php content unless it's on the same server.
Make sure you're trying to access a locally hosted file and not something remote and it should work.
Also if you try to echo code it will try to parse it, so pass it through htmlspecialchars($source) and it should work.
Something like this:
<?php
echo "<pre>";
echo htmlspecialchars(file_get_contents('file.php'));
echo "</pre>";
?>
Would echo formatted source code of the php file, including comments and any other text in it without being parsed. And since it looks like it's important to you, I'd also say that it shows in the DOM of course since it's no longer code, now it's text. You can place it inside a container, style it and do whatever you want with it.
You can also do :
<?php
highlight_file('file.php');
// or alternatively
echo highlight_file('file.php',true);
And that will output the file like with htmlspecialchars and file_get_content but within <code> tags and with some syntax highlighting.
highlight_string :
(PHP 4, PHP 5, PHP 7)
highlight_string — Syntax highlighting of a string
highlight_file :
(PHP 4, PHP 5, PHP 7)
highlight_file — Syntax highlighting of a file
Related
How would one go about showing PHP code on user end. Sort of like w3School does?
Having lets say a grey area div, and then showing the code in there without activating it?
You can use html entities <?php in the html it will be rendered as <?php
You can use htmlspecialchars to encode your code to use html entities.
Use <pre> or <code> tags to wrap your code.
Take a look at http://php.net/manual/en/function.highlight-string.php to further see how you can make the code look pretty.
Since passing a large block of code to highlight_string() can be messy, you may want to look at output buffering in combination with highlight_string to output colorized php code.
Something like:
<?php
ob_start();
?>
phpinfo();
echo "this echo statement isn't executed";
<?php
$code = ob_get_clean();
highlight_string($code);
?>
Simply you can use following code to display php code on webpage.
highlight_string("<?php print('This is php code.'); ?>");
It will give output like
<?php print('This is php code.'); ?>
The first step is to not wrap that code in PHP tags. So instead of this:
<?
var sample = "code";
?>
You would have this:
var sample = "code";
It's not the code itself which triggers the server-side compile from the PHP engine, it's the tags which indicate to that engine what blocks of the file are code and what are not. Anything that's not code is essentially treated as a string and output to the page as-is for the browser to interpret.
Once you're outputting the code, it's then a matter of formatting it. The old standard is to wrap it in pre tags to get rid of HTML-ish formatting:
<pre>
var sample = "code";
</pre>
You can also apply CSS style to the pre tags (or any other tags you want to use for displaying code, such as div) as you see fit.
There are also very useful code syntax highlighting plugins and tools to make the code a lot "prettier". Google-code-prettify often comes highly recommended.
Typically this is done by showing code within <pre> or <code> tags.
You can use this template........
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
It will show the source code and output following.
use the header function of php, this will rea
<?php
header("content-type: text/plain");
?>
The PHP code will just be a string that you can echo or print onto the page, no different than any other data you want PHP to display for you. If you want to keep the formatting (ex. the indentation), put it inside a <pre><code> block.
Ex:
$php_code = '<?php $foo = bar; ?>';
echo "<pre><code>$php_code</code></pre>";
I tried to use short echo tags i.e. <?= ?> to display a string into my PHP file which already has PHP code present within <?php ?> tags. So, I tried to use the short echo tag within my code and got parse error.
See below code :
<?php
echo 'if you want to serve PHP code in XHTML or XML documents,
use these tags';
<?= 'While this is going to be parsed.'; ?>
?>
I got following output :
Parse error: syntax error, unexpected '<', expecting end of file in
So, my question is: Are the short echo tags intended to be used only when PHP code is going to embed into HTML code and that's too not within already existing php pair of tags?
Can's I use in a file which contains pure PHP code only?
Yes you can use Short tag in a file which contains pure PHP code only. Here you are using php opening short tag inside php tags, that's why its throwing Parse Error.
if yo want to use, then you should try like below .
<?php
echo 'if you want to serve PHP code in XHTML or XML documents,
use these tags';
?>
<?= 'While this is going to be parsed.'; ?>
Use short tag alone. Currently you are using short tag inside php tag.
I doesn't cause error if it is used alone in html.
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.
I think what I'm trying to do must be impossible; incredibly difficult; or pretty easy, and I just don't know how to express what I want correctly to Google.
What I want to do is the following:
For the purposes of templating, I am outputting HTML to a file. However, I want the file to contain PHP tags - not the parsed value of the PHP code (this value will change based upon included scripts in the output file) - but the actual <?php ?> tags themselves - containing the unmodified code, such that the code is still executable at run time.
This is an example of what I want to get outputted:
<html>
<body>
<img src="<?php echo IMG_PATH;?>img.jpg" />
</body>
</html>
What I'm getting is a combination of things that are wrong, depending on which method I use. Basically, I need to be able to write parsable PHP code to a HTML file. Now, whether or not this is a good idea is debatable, and a subject I'm not too interested in at the moment. I just want to know if a sane solution exists.
I came up with the following insane solution:
$content='<html><body><img src="zxzxzx?php echo IMG_PATH; ?qjqjqj" />';
file_put_contents($file,$content);
$command='perl -p -i.bktmp -e "s/zxzxzx/</g" '.$file.' 2>'.$path.'error.log';
$command2='perl -p -i.bktmp -e "s/qjqjqj/>/g" '.$file.' 2>'.$path.'error.log';
exec($command);
exec($command2);
Now, on one hand, this feels devilishly clever. And, it also feels insane. Is there a better/possible way to do this?
You are misinterpreting how PHP operates. The following code:
<?php
echo 'This string <?php echo 'contains' ?> some PHP code';
Will produce as output
This string <?php echo 'contains' ?> some PHP code
You will NOT get:
This string contains some PHP code
PHP's parser is not recursive. PHP code embedded inside PHP code, e.g. inside a string, as it is above, is NOT treated as PHP code - it'll just be some text.
If you want to write out some PHP code, then just write it out:
$string = 'This string <?php echo 'contains' ?> some PHP code';
file_put_contents('file.php', $string);
You don't need to take any special measures for the PHP code in the string to be written out, it'll just "work".
Now, that being said, if you ever put that string into a context where it COULD be evaluated as code, e.g.
eval($string);
then you WILL have to take measures to keep PHP from seeing the opening <?php tag and switching into "code mode".
Instead your HTML file needs to be a PHP file. See below:
File: test.php
<html>
<head>
</head>
<body>
<? echo 'My PHP embedded in HTML'; ?>
<img src="<? echo $imagePath; ?>"/>
</body>
</html>
I m using the php function file_get_contents to parse a php file. But it seems that as soon as it is reading the php tags the file_get_contents is malfunctioning.
I checked the function with a normal text file, its functioning perfectly. But even if it finds php tags in a text file, the file is being half read. How can i find a way to get the full contents.
Is the file local? Or are you trying to get a remote file? How did you check that the content is not read? Echoing it to a browser might trick you because of the < char in <?php
Use htmlspecialchars or <pre> to view the whole text. Or just look at the source of the page.