getting data from php variable - php

$content = file_get_contents('file.php');
echo $content;
nothing displays, expect when displaying the page sourcecode in browser the display is this
<? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>
so it wont execute the script when getting the content..
file.php contains this code
<? foreach(glob("folder/*.php") as $class_filename) {
require_once($class_filename);
}
?>
and if I do next
$content = foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>
it complains about unexpected foreach...
is there a way to read the folder/.php files content to single $variable and then echo/print all folder/.php files to page where it should be?
thanks for help already.

Is that what you want to do ?
$content = '';
foreach (glob('folder/*.php') as $class){$content .= file_get_contents($class);}
echo $content;

What you're trying won't execute the contents of the "file.php", jsut display the contents of them on screen.
If you want to execute file.php, use eval ($content)
To capture the output, use something like:
ob_start(); // Don't echo anything but buffer it up
$codeToRun=file_get_contents('file.php'); // Get the contents of file.php
eval ($codeToRun); // Run the contents of file.php
$content=ob_get_flush(); // Dump anything that should have been echoed to a variable and stop buffering
echo $content; //echo the stuff that should have been echoed above

Related

php call variable and put back on while loop

sorry for my last question where i try put some live code with ob_start buffer content is not helping me to solve my problem because buffer content just collects output text, it doesn't execute any code. thanks #akrys for your advices
what i want is to put code into while looping like this
$sql = $conn->query("SELECT * FROM `users`");
$var = $row['full_name'];
include('test.php');
after i call test.php contain while code like:
while($row = $sql->fetch_array()) {
echo $var;
}
everything is work if i replace $var with $row['full_name'];
but i get the name of row field from some script on index.php so i should access that file first then i call portable file contain query to fetch_array on test.php
how to make it work when i put it back with $var contain variable field name
thank you very much for your attention guys
you should to include before your code
page
test.php
<?php
$someVariable = 'hello'; // the variable only can access in here
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable $someVariable";
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
of course you can use define too
page test.php
<?php
define( "SOMEVARIABLE", hello );
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable ".SOMEVARIABLE;
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
you can use:
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
for more help, use the link below:
enter link description here

Include a php file, but return output as a string instead of printing

I want to include a file, but instead of printing output I want to get it as string.
For example, I want include a file:
<?php echo "Hello"; ?> world!
But instead of printing Hello world! while including the file I want to get it as a string.
I want to filter some elements from the file, but not from whole php file, but just from the html output.
Is it possible to do something like this?
You can use php buffers like this:
<?php
ob_start();
include('other.php');
$script = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
EDIT: You can abstract this into a function:
function inlcude2string($file) {
ob_start();
include($file);
$output = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
return $output;
}
$str = inlcude2string('other.php');

How can I replace braces with <?php ?> in php file?

I wanna replace braces with <?php ?> in a file with php extension.
I have a class as a library and in this class I have three function like these:
function replace_left_delimeter($buffer)
{
return($this->replace_right_delimeter(str_replace("{", "<?php echo $", $buffer)));
}
function replace_right_delimeter($buffer)
{
return(str_replace("}", "; ?> ", $buffer));
}
function parser($view,$data)
{
ob_start(array($this,"replace_left_delimeter"));
include APP_DIR.DS.'view'.DS.$view.'.php';
ob_end_flush();
}
and I have a view file with php extension like this:
{tmp} tmpstr
in output I save just tmpstr and in source code in browser I get
<?php echo $tmp; ?>
tmpstr
In include file <? shown as <!--? and be comment. Why?
What you're trying to do here won't work. The replacements carried out by the output buffering callback occur after PHP code has already been parsed and executed. Introducing new PHP code tags at this stage won't cause them to be executed.
You will need to instead preprocess the PHP source file before evaluating it, e.g.
$tp = file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$tp = str_replace("{", "<?php echo \$", $tp);
$tp = str_replace("}", "; ?>", $tp);
eval($tp);
However, I'd strongly recommend using an existing template engine; this approach will be inefficient and limited. You might want to give Twig a shot, for instance.
do this:
function parser($view,$data)
{
$data=array("data"=>$data);
$template=file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$replace = array();
foreach ($data as $key => $value) {
#if $data is array...
$replace = array_merge(
$replace,array("{".$key."}"=>$value)
);
}
$template=strtr($template,$replace);
echo $template;
}
and ignore other two functions.
How does this work:
process.php:
<?php
$contents = file_get_contents('php://stdin');
$contents = preg_replace('/\{([a-zA-Z_][a-zA-Z_0-9]*)\}/', '<?php echo $\1; ?>', $contents);
echo $contents;
bash script:
process.php < my_file.php
Note that the above works by doing a one-off search and replace. You can easily modify the script if you want to do this on the fly.
Note also, that modifying PHP code from within PHP code is a bad idea. Self-modifying code can lead to hard-to-find bugs, and is often associated with malicious software. If you explain what you are trying to achieve - your purpose - you might get a better response.

php load up a .php page and replace variable, and then store the output

I would like to load different .php page (the .php contain html and some php variable need to be replaced.
For example:
load.php
$output = '';
load the test.php and replace that $this->name with value.
store the html to $output
load the test1.php and replace that $this->name with value.
append to the previous $output variable
so at the end i would have a $output variable have all the updated html
Any suggestion is appreciated.
test.php
>
<html>
<?php echo $this->name; ?>
</html>
test1.php
>
<html>
<?php echo $this->address; ?>
</html>
You likely want to use output buffering with a require or include statement:
ob_start();
require('load.php');
$output = ob_get_contents();
ob_end_clean();
$output should contain the contents of load.php with any variables processed.
To process multiple files (or anything else) just run it all between ob_start() and the last two lines, so you could grab two files like so:
ob_start();
require('test.php');
require('test1.php');
$output = ob_get_contents();
ob_end_clean();

Storing an html page into a php variable [duplicate]

This question already has answers here:
HTML into PHP Variable (HTML outside PHP code)
(7 answers)
Closed 4 years ago.
Hi i'd like to store a dinamically generated(with php) html code into a variable and be able to send it as a reply to an ajax request.
Let's say i randomly generate a table like:
<?php
$c=count($services);
?>
<table>
<?php
for($i=0; $i<$c; $i++){
echo "<tr>";
echo "<td>".$services_global[$i][service] ."</td>";
echo "<td>".$services_global[$i][amount]."</td>";
echo "<td>€ ".$services_global[$i][unit_price].",00</td>";
echo "<td>€ ".$services_global[$i][service_price].",00</td>";
echo "<td>".$services_global[$i][service_vat].",00%</td>";
echo "</tr>";
}
?>
</table>
I need to store all the generated html code(and the rest) and echo it as a json encoded variable like:
$error='none';
$result = array('teh_html' => $html, 'error' => $error);
$result_json = json_encode($result);
echo $result_json;
I could maybe generate an html file and then read it with:
ob_start();
//all my php generation code and stuff
file_put_contents('./tmp/invoice.html', ob_get_contents());
$html = file_get_contents('./tmp/invoice.html');
But it sounds just wrong and since i don't really need to generate the code but only send it to my main page as a reply to an ajax request it would be a waste of resources.
Any suggestions?
You don't have to store it in a file, you can just use the proper output buffering function
// turn output buffering on
ob_start();
// normal output
echo "<h1>hello world!</h1>";
// store buffer to variable and turn output buffering offer
$html = ob_get_clean();
// recall the buffered content
echo $html; //=> <h1>hello world!</h1>
More about ob_get_clean()
if the data is so much expensive to regenerate then I would suggest you to use memcached.
Otherwise I would go regenerate it every-time or cache it on the frontend.
for($i=0;$i<=5;$i++)
{
ob_start();
$store_var = $store_var.getdata($i); // put here your recursive function name
ob_get_clean();
}
function getdata($i)
{
?>
<h1>
<?php
echo $i;
?>
</h1>
<?php
ob_get_contents();
}

Categories