修改现有的PHP函数以返回一个字符串 [英] Modify an Existing PHP Function to Return a String
问题描述
<?php
function get_header(){
?>
< div id =header>
< div class =page-width>
<! - 在这行之后有更多的HTML。 - >
<?php
}
?>
因此,当我调用 get_header()
,该函数输出HTML。
调整此函数以将HTML作为字符串返回的最简单方法是什么?我是否需要为此功能创建一个包装?换句话说,我希望能够做到 var html_string = get_header_wrapper()
,其中 html_string
包含上述所有HTML。
我能想到的一件事是复制函数并使其返回一个字符串。然而,这样做效率太低,因为它引入了很多代码重复。
<?php
function get_header_wrapper (){
var ret =<<<< EOD
< div id =header>
< div class =page-width>
<! - 在这行之后有更多的HTML。 - >
...
EOD;
return ret;
}
?>
您可以使用输出缓冲 Docs 获取输出那个函数:
ob_start();
get_header();
$ html = ob_get_clean();
如果您不止一次需要,可以将其包装为自己的函数:
/ **
*调用函数并以字符串形式返回它的输出。
*
* @param回调$ function
* @param array $ arguments(可选)
* @param var $ return(可选)回调函数的返回值
$ @ b $ b * /
函数ob_get_call($ function,array $ arguments = array(),& $ return = NULL)
{
ob_start() ;
$ return = call_user_func_array($ function,$ arguments);
$ buffer = ob_get_clean();
返回$ buffer;
}
用法:
$ html = ob_get_call('get_header');
由于今天的答案很流行,下面是另一个获得include输出的函数: / p>
/ **
*包含一个文件并以字符串形式返回它的输出。
*
* @param字符串$文件
* @param数组$变量(可选)键作为变量名称和值作为变量值
* @param var $ includeReturn(可选) include
* @return字符串函数输出的返回值
* /
函数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();
}
用法:
include.php
:
< div class =greeting >
您好< em><?php echo htmlspecialchars($ name); ?>< / EM> ;!
< / div>
使用:
$ variables = array(
'name'=>'Marianne',
);
$ html = ob_get_include('include.php',$ vars);
相关:
I have a simple PHP function that outputs HTML.
<?php
function get_header() {
?>
<div id="header">
<div class="page-width">
<!-- And a lot more HTML after this line. -->
<?php
}
?>
So, when I call get_header()
, the function outputs the HTML.
What is the simplest option to tweak this function to return the HTML as a string? Do I need to create a wrapper around this function? In other words, I'd like to be able to do e.g. var html_string = get_header_wrapper()
, where html_string
contains all the HTML above.
One thing I could think of is to duplicate the function and make it return a string. However, that would be so inefficient because it introduces a lot of code duplicate.
<?php
function get_header_wrapper() {
var ret = <<<EOD
<div id="header">
<div class="page-width">
<!-- And a lot more HTML after this line. -->
...
EOD;
return ret;
}
?>
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函数以返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!