How to return raw php code? - php

I have this code for example:
ob_start();
echo "hello there";
$output = ob_get_contents();
return $output;
When I run it, I get back:
hello there
But how can I get back
echo "hello there";
Is there a way to do this easily?

To output arbitrary text as-is you can close the PHP script and then reopen it. Anything between the closing and opening tags is output as-is
ob_start();
?>echo "hello there";
<?php
$output = ob_get_contents();
return $output;

ob_get_contents will return the echoed output, so you can't use it to show actual code.
To simply print out code, I would try this:
$code = file_get_contents('your_code.php');
echo "<pre>{$code}</pre>";
Also you can write you code separatle as text and echo it or eval (if you need execution).
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
result:
This is a $string with my $name in it.
This is a cup with my coffee in it.

By representing it as a string:
ob_start();
$str = <<<STR
echo "hello there";
STR;
echo $str;
$output = ob_get_contents();
return $output;

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.

How do i make php tag readable in a string?

I'm try to run php tag inside a string like so:
$str = '<h1><?php echo Hello World ?></h1>';
echo $str;
But it returns
<h1><!-- ?php echo Hello World ? --></h1>
I want an output similar to this code:
ob_start();
require ("../view/file_name.inc");
$html = ob_get_contents();
ob_end_clean();
Without Using:
require
or
include
Using:
file_get_contents('../view/file_name.inc');
without templating as well
let say :
file_name.inc has this:
<h1><?php echo $title ?></h1>
<?php foreach($data as $d){ ?>
<p><?php echo $d->desc ?><p>
<?php } ?>
The output something like:
<h1>Hello</h1>
<p>desc1</p>
<p>desc2</p>
Use htmlentities function of PHP to achieve your goal.
$str = '<h1><?php echo Hello World ?></h1>';
echo htmlentities($str);
It will output
<h1><?php echo Hello World ?></h1>
EDIT
Use eval to execute php script in string. try below code
$str = '<h1><?php echo "Hello World" ?></h1>';
eval('?>'.$str.'<?php;');
echo $str;
this should be help you
$str = '<h1>Hello World</h1>';

preg_replace not working in for loop

I want to include files using shortcode, but problem is that preg_replace isn't working properly in for loop.my code is below:
<?php
$output = "[#include('file1')] [#include('file2')]";
if(preg_match_all("/\[\#include\(\'(.*?)\'\)\]/", $output, $match)){
for($i=0;$i<count($match);$i++){
$output = preg_replace("/\[\#include\(\'(.*?)\'\)\]/", $match[1][$i], $output);
}
}
echo $output;
the above code prints "file1 file1", it should be print "file1 file2" both file name but it is not printing the both files name.Please tell where i'm wrong.
What you're looking for is already in the output array of your regular expression (i.e. in the $match array), you just need to get it using implode()!
<?php
$output = "[#include('file1')] [#include('file2')]";
preg_match_all("/\[\#include\(\'(.*?)\'\)\]/", $output, $match);
echo $out = implode(' ', $match[1]);
?>

PHP - Find and replace enclosed text in an external HTML file

I have a PHP file that renders an HTML file, inside this HTML file I have this piece of code
<div class="app">{: echo $this->content :}</div>
And I want to replace the opening {: and closing :} tags with the traditional <?php ?> tags to make it look something like this:
<div class="app"><?php echo $this->content ?></div>
$contents = file_get_contents ("file.php");
$contents = str_replace(array('{:', ':}'), array('<?php', '?>'), $contents);
file_put_contents("file.php", $contents);
You could do a simple string replace if you are doing this within PHP
str_replace(array('{:', ':}'), array('<?php', '?>'), $file_content)
Try this :
$string = '<div class="app">{: echo $this->content :}</div>';
$string = str_replace('{:','<?php',$string);
$string = str_replace(':}','?>',$string);
If you want to catch the content inside the you can use this :
$string = '<div class="app">{: echo $this->content :}</div>';
preg_match('/<div class="app">(.+)<\/div>/',$string,$preg_array);
$string = str_replace('{:','<?php',$preg_array[1]);
$string = str_replace(':}','?>',$string);
Output :
<?php echo $this->content ?>

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