Modify an Existing PHP Function to Return a String
You can make use of output bufferingDocs to get the output of that function:
ob_start();
get_header();
$html = ob_get_clean();
If you need that more than once, you can wrap it into a function of it's own:
/**
* call a function and return it's output as string.
*
* @param callback $function
* @param array $arguments (optional)
* @param var $return (optional) the return value of the callback
* @return string function output
*/
function ob_get_call($function, array $arguments = array(), &$return = NULL)
{
ob_start();
$return = call_user_func_array($function, $arguments);
$buffer = ob_get_clean();
return $buffer;
}
Usage:
$html = ob_get_call('get_header');
As the answer is that popular today, here is another function to get the output of an include:
/**
* include a file and return it's output as string.
*
* @param string $file
* @param array $variables (optional) keys as variable names and values as variable values
* @param var $includeReturn (optional) the return value of the include
* @return string function output
*/
function ob_get_include($file, array $variables = array(), &$includeReturn = NULL)
{
$includeFilename = $file;
unset($file);
extract($variables);
unset($variables);
ob_start();
$includeReturn = include($includeFilename);
return ob_get_clean();
}
Usage:
include.php
:
<div class="greeting">
Hello <em><?php echo htmlspecialchars($name); ?></em>!
</div>
Using:
$variables = array(
'name' => 'Marianne',
);
$html = ob_get_include('include.php', $vars);
Related:
- Answer to *Load result of php code instead of the code as a string
- Answer to Is include()/require() with “side effects” a bad practice?
PHP Function to return string
You should simply store the return value in a variable:
$deliveryPrice = getDeliveryPrice(12);
echo $deliveryPrice; // will print 20
The $deliveryPrice
variable above is a different variable than the $deliveryPrice
inside the function. The latter is not visible outside the function because of variable scope.
Modify string, PHP
Something like this:
$final = substr($initial,0,4).'/'.substr($initial,4,2).'/'.substr($initial,6,2)
Use substr.
modify a method/function at runtime
Look into anonymous functions. If you can run PHP 5.3 that might be more along the lines of what you're trying to do.
PHP Modify a single line in a text file
This should works! :)
$file = "./users.txt";
$fh = fopen($file,'r+');
// string to put username and passwords
$users = '';
while(!feof($fh)) {
$user = explode(',',fgets($fh));
// take-off old "\r\n"
$username = trim($user[0]);
$password = trim($user[1]);
// check for empty indexes
if (!empty($username) AND !empty($password)) {
if ($username == 'mahdi') {
$password = 'okay';
}
$users .= $username . ',' . $password;
$users .= "\r\n";
}
}
// using file_put_contents() instead of fwrite()
file_put_contents('./users.txt', $users);
fclose($fh);
Return Results of a Function Within a Variable to Change the Varible
I did manage to solve this.
${'omni'.posicionnavbar($tabs['position'])} .= $nuevo_tab;
thanks to PHP Cookbook
Knowing the proper symbols to link the variables together, I got rid of the switch and function altogether. The way I have this set up ${'omni'.$tabs['position']} .= $nuevo_tab;
is enough to get the job done.
how to get the result from a php file into a string
<?php
ob_start();
include 'template.php';
$result = ob_get_clean()
?>
this should do, the $result is the string you need
Related Topics
What Means Call to a Member Function on Boolean and How to Fix
How to Create Clean Url Using .Htaccess
How to Use an Object Method as a Callback Function
Nested PHP Ternary Trouble: Ternary Output != If - Else
How to Sort a Multidemensional Array by an Inner Key
How to Ipc Between PHP Clients and a C Daemon Server
Should I Be Using Assert in My PHP Code
Protocol Https Not Supported or Disabled in Libcurl
How to Read a .Txt File with PHP
Replace Values in a Uri Query String
How to Get Around or Make PHP JSON_Decode Not Alter My Very Large Integer Values