How to get raw php content? - php

I'm trying to get file content from a php file.
$content = file_get_contents('test.php');
var_dump($content);
//outputs
addUser();
test.php
<?php
$client->addUser();
Why is file_get_contents removing $client-> ?

It's probably not removing anything, but you're reading the result in a browser, and the browser thinks that anything between a < and a > is a html tag.
Use the "view source" function in your browser to see the whole thing.

You've got 2 HTML brackets <>
Try this
echo htmlentites($content);

Related

Render html to page from database PHP [duplicate]

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

remove <> automaticly in my php files

I call for example this element SEND_EXTRA_ORDER_EMAILS_TO form my database
SEND_EXTRA_ORDER_EMAILS_TO = test<test#test.com>
when I write in my php code an echo, print_r or var_dump
eg
var_dump(SEND_EXTRA_ORDER_EMAILS_TO) it write only test.
do you have an idea how to resolve this element be cause it's make to send an email
I use php 7 and mysql 7
Tk
You can use htmlspecialchars() to escape the string so your browser wont treat your string as HTML.
$code = "<h1>Hello world</h1>";
echo htmlspecialchars($code);
// Will output <h1>Hello world</h1> without rendering the HTML in the browser
try with that htmlentities(EMAIL_FROM) and works

PHP remove <body><html>...</html></body> from echo output

I have a php script that does a query in my database and returns a string ( like "2" ). I print it using
print strip_tags('2');
but in the output of my browser I get :
<body><html>2</html></body>
Is there any way to prevent the tags from beiing printed? Is it maybe that the browser auto adds them?
For all those answering about strip_tags (" 2 ");
THIS IS WRONG:
I want a siple version.php
with
echo '2';
and nothing else. It prints the tags too. I don't have the tags and then try to print.
More explanation to those who try to get easy rep
my code is:
$str = '2';
print strip_tags($str);
and it prints
<html><head></head><body>2</body></html>
It is not possible. The browser creates these elements automatically, without it there would not be any text flow(means nothing of this could be made visible). You can just use this variable for any script, it won't include the HTML tags. This is only made by the browser to make it visible for you.
You can use
header("Content-Type: text/plain");
at the beginning of your script, in order to tell the browsers you're only gonna send plain text, not html. This will prevent your browser from automatically adding those html tags.
Then, check what you print (or echo). Here, the body tag should be in html tag.

Get text from URL txt

Is there any way to get a simple php code that just displays the text from a txt document, that's on a url?
This is what I've got so far. I'm sure I'm missing something~
<?
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
fopen ($text)
?>
Guessing you can't open it, since it's not on the drive. Any workaround that, or fix you guys could help me with?
Thanks (:
Too easy! :)
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
Explanation:
file_get_contents() will return the contents of the remote file and assign it to the $text variable. Then you'll just have to ouput these contents using the echo statement
Alternative: use the readfile() function. It will output the contents of the file directly:
readfile('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
You don't have to open it. It's already appended as a string to the variable $text.
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
//Outputs "“Why Don't You Get A Job?” ― The Offspring"

php: file_get_contents is stripping out php code

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

Categories