PHP 语法糖:如何在给定输入上多次应用一个函数?
PHP syntactic sugar: How to apply a function on a given input multiple times?
我从数据库中得到一条文本,其中函数 htmlentities()
被应用了四次。示例文本:
specials & workshops
为了解码此文本,我必须执行以下操作:
$out = html_entity_decode(html_entity_decode(html_entity_decode(html_entity_decode("specials & workshops"))));
结果:
specials & workshops
在 PHP 中有没有自然的方法可以更有效地编写此代码?
为什么不声明一个函数来这样做?
$in = "specials & workshops";
$decode = function($in) {
foreach(range(1,4) as $x) $in = html_entity_decode($in); return $in; };
function decode($in) {
foreach(range(1,4) as $x)
$in = html_entity_decode($in);
return $in;
}
// inline
$out = $decode($in);
// traditional
$out = decode($in);
根据@JayBlanchard 的递归思想,我没有创建以下内容 - 非常喜欢它:
/**
* Apply a function to a certain input multiple times.
*
* @param $input: The input variable:
* @param callable $func: The function to call.
* @param int $times: How often the function should be called. -1 for deep call (unknown number of calls required). CAUTION: If output always changes this results in an endless loop.
* @return mixed
*/
function recapply($input,callable $func,int $times) {
if($times > 1) {
return recapply($func($input),$func,$times - 1);
} else if($times == -1) {
$res = $func($input);
if($res === $input) {
return $input;
} else {
return recapply($res,$func,-1);
}
}
return $func($input);
}
工作示例调用:
echo recapply("specials & workshops","html_entity_decode",4);
我喜欢以这样一种方式递归地进行,我不需要知道要匹配多少个实体。
$string = 'specials & workshops';
$entity = '/&/';
function recurseHTMLDecode($str, $entity) {
preg_match($entity, $str, $matches);
echo count($matches);
if(1 == count($matches)) {
$str = html_entity_decode($str);
$str = recurseHTMLDecode($str, $entity);
return $str;
} else {
return $str;
}
}
var_dump(recurseHTMLDecode($string, $entity));
这个returns:
11110string(20) "specials & workshops"
这是EXAMPLE
这可以通过向函数添加实体白名单来改进,这样您在调用时就不必指定实体,只需遍历白名单即可。这将解决字符串中有多个实体的问题。它可能非常复杂。
我从数据库中得到一条文本,其中函数 htmlentities()
被应用了四次。示例文本:
specials & workshops
为了解码此文本,我必须执行以下操作:
$out = html_entity_decode(html_entity_decode(html_entity_decode(html_entity_decode("specials & workshops"))));
结果:
specials & workshops
在 PHP 中有没有自然的方法可以更有效地编写此代码?
为什么不声明一个函数来这样做?
$in = "specials & workshops";
$decode = function($in) {
foreach(range(1,4) as $x) $in = html_entity_decode($in); return $in; };
function decode($in) {
foreach(range(1,4) as $x)
$in = html_entity_decode($in);
return $in;
}
// inline
$out = $decode($in);
// traditional
$out = decode($in);
根据@JayBlanchard 的递归思想,我没有创建以下内容 - 非常喜欢它:
/**
* Apply a function to a certain input multiple times.
*
* @param $input: The input variable:
* @param callable $func: The function to call.
* @param int $times: How often the function should be called. -1 for deep call (unknown number of calls required). CAUTION: If output always changes this results in an endless loop.
* @return mixed
*/
function recapply($input,callable $func,int $times) {
if($times > 1) {
return recapply($func($input),$func,$times - 1);
} else if($times == -1) {
$res = $func($input);
if($res === $input) {
return $input;
} else {
return recapply($res,$func,-1);
}
}
return $func($input);
}
工作示例调用:
echo recapply("specials & workshops","html_entity_decode",4);
我喜欢以这样一种方式递归地进行,我不需要知道要匹配多少个实体。
$string = 'specials & workshops';
$entity = '/&/';
function recurseHTMLDecode($str, $entity) {
preg_match($entity, $str, $matches);
echo count($matches);
if(1 == count($matches)) {
$str = html_entity_decode($str);
$str = recurseHTMLDecode($str, $entity);
return $str;
} else {
return $str;
}
}
var_dump(recurseHTMLDecode($string, $entity));
这个returns:
11110string(20) "specials & workshops"
这是EXAMPLE
这可以通过向函数添加实体白名单来改进,这样您在调用时就不必指定实体,只需遍历白名单即可。这将解决字符串中有多个实体的问题。它可能非常复杂。