Php 如何连接函数输出?
任务是获取以下信息:Php 如何连接函数输出?,php,syntax,Php,Syntax,任务是获取以下信息: Nel mezzo del cammin di nostra vita 由此: nel-MEZzo---del-cammin, <strong>BRAKELINE</strong>di nostra-vita. nel MEZzo--del cammin,BRAKELINEdi nostra vita。 我真的很讨厌这种语法: function custom_function($v){ return str_replace("BRAKE
Nel mezzo del cammin
di nostra vita
由此:
nel-MEZzo---del-cammin, <strong>BRAKELINE</strong>di nostra-vita.
nel MEZzo--del cammin,BRAKELINEdi nostra vita。
我真的很讨厌这种语法:
function custom_function($v){
return str_replace("BRAKELINE","\n",$v);
}
$SRC = ' nel-MEZzo---del-cammin, <strong>BRAKELINE</strong>di nostra-vita. ';
$v = ucfirst(trim(strtolower(custom_function(preg_replace('/[^\w]+/',"\040",strip_tags($SRC))))));
echo $v;
函数自定义函数($v){
返回str_替换(“BRAKELINE”、“\n”、$v);
}
$SRC='nel MEZzo---del cammin,BRAKELINEdi nostra vita';
$v=ucfirst(修剪(strtolower(自定义_函数)(preg_replace('/[^\w]+/'),“\040”,带标签($SRC‘’)));
echo$v;
我尝试了一些新的东西:
function ifuncs($argv=NULL,$funcs=array()){
foreach($funcs as $func){
$argv = $func($argv);
}
return $argv;
}
$SRC = ' nel-MEZzo---del-cammin, <strong>BRAKELINE</strong>di nostra-vita. ';
$v = ifuncs(
$SRC,
array(
'strip_tags',
'f1' => function($v){ return preg_replace('/[^\w]+/',"\040",$v); },
'f2' => function($v){
return str_replace("BRAKELINE","\n",$v);
},
'strtolower',
'trim',
'ucfirst'
)
);
echo $v;
函数ifuncs($argv=NULL,$funcs=array()){
foreach($func作为$func){
$argv=$func($argv);
}
返回$argv;
}
$SRC='nel MEZzo---del cammin,BRAKELINEdi nostra vita';
$v=ifuncs(
$SRC,
排列(
“带标签”,
'f1'=>函数($v){return preg_replace('/[^\w]+/',“\040”,$v);},
'f2'=>函数($v){
返回str_替换(“BRAKELINE”、“\n”、$v);
},
“strtolower”,
“修剪”,
“ucfirst”
)
);
echo$v;
它工作得很好,但我想知道是否有更好的方法(也称为现有库)来做到这一点。您所写的是如何工作的:
不是更短,而是更优雅
function fns($input=NULL,$fns=array()){
$input = array_reduce($fns, function($result, $fn) {
return $fn($result);
}, $input);
return $input;
}
function ifuncs(array $funcs, $input = null)
{
return array_reduce($funcs, function($result, $fn) {
return $fn($result);
}, $input));
}
function fns($input=NULL,$fns=array()){
$input = array_reduce($fns, function($result, $fn) {
return $fn($result);
}, $input);
return $input;
}