<?php
/**
* 函数用于从指定数组返回一个值或设定的默认值
* 原文:Utility function to return a value from a named array or a specified default
*
* @copyright (C) Mambo Foundation Inc.
* @param array $arr 要获取数据的数组
* @param string $name 数组下标的名称
* @param mixed $def 默认值
* @param int $mask 区分不同任务
* @return mixed $return
*/
define( "_NO_TRIM", 0x0001 ); // 不使用 trim() 函数
define( "_ALLOW_HTML", 0x0002 ); // 允许使用 HTML 代码
define( "_ALLOW_RAW", 0x0004 ); // 使用原始数据
function getParam(&$arr, $name, $def=null, $mask=0) {
if (isset($arr[$name])) {
if (is_array($arr[$name])) {
foreach ($arr[$name] as $key => $element) {
$result[$key] = getParam($arr[$name], $key, $def, $mask);
}
} else {
$result = $arr[$name];
if (!($mask & _NO_TRIM)) {
$result = trim($result);
}
if (!is_numeric($result)) {
if (!($mask & _ALLOW_HTML)) {
$result = strip_tags($result);
}
if (!($mask & _ALLOW_RAW)) {
if (is_numeric($def)) {
$result = intval($result);
}
}
}
if (!get_magic_quotes_gpc()) {
$return = addslashes($return);
}
}
return $result;
} else {
return $def;
}
}
?>
调用方法:
<?php
$words = getParam($_GET, 'words', 'hello');
$arr = getParam($_POST, 'arr');
$text = getParam($_REQUEST, 'text', '', 2);
...
?>