我正在努力通过David Powers OO Solutions并需要帮助理解他的Pos_Validator类中的一种方法。
方法名称暗示它处理数组但是,第一个参数需要 单个 字段名称?其余的args是可选的。我在这里错过了什么?
/**
* Sanitizes array by removing completely all tags (including PHP and HTML).
*
* The second and third optional arguments determine whether the ampersand
* and quotes are converted to numerical entitites. By default, ampersands are
* not converted, but both double and single quotes are replaced by numerical
* entities.
*
* Arguments four to seven determine whether characters with an ASCII value less than 32 or
* greater than 127 are encoded or stripped. By default, they are left untouched.
*
* @param string $fieldName Name of submitted value to be checked.
* @param boolean $encodeAmp Optional; converts & to & if set to true; defaults to false.
* @param boolean $preserveQuotes Optional; preserves double and single quotes if true; defaults to false.
* @param boolean $encodeLow Optional; converts ASCII values below 32 to entities; defaults to false.
* @param boolean $encodeHigh Optional; converts ASCII values above 127 to entities; defaults to false.
* @param boolean $stripLow Optional; strips ASCII values below 32; defaults to false.
* @param boolean $stripHigh Optional; strips ASCII values above 127; defaults to false.
*/
public function removeTagsFromArray($fieldName, $encodeAmp = false, $preserveQuotes = false, $encodeLow = false, $encodeHigh = false, $stripLow = false, $stripHigh = false)
{
// Check that another validation test has not been applied to the same input
$this->checkDuplicateFilter($fieldName);
// Set the filter options
$this->_filterArgs[$fieldName]['filter'] = FILTER_SANITIZE_STRING;
// Multiple flags are set using the "binary or" operator
$this->_filterArgs[$fieldName]['flags'] = FILTER_REQUIRE_ARRAY;
if ($encodeAmp) {
$this->_filterArgs[$fieldName]['flags'] |= FILTER_FLAG_ENCODE_AMP;
}
if ($preserveQuotes) {
$this->_filterArgs[$fieldName]['flags'] |= FILTER_FLAG_NO_ENCODE_QUOTES;
}
if ($encodeLow) {
$this->_filterArgs[$fieldName]['flags'] |= FILTER_FLAG_ENCODE_LOW;
}
if ($encodeHigh) {
$this->_filterArgs[$fieldName]['flags'] |= FILTER_FLAG_ENCODE_HIGH;
}
if ($stripLow) {
$this->_filterArgs[$fieldName]['flags'] |= FILTER_FLAG_STRIP_LOW;
}
if ($stripHigh) {
$this->_filterArgs[$fieldName]['flags'] |= FILTER_FLAG_STRIP_HIGH;
}
}
答案 0 :(得分:1)
自从我写这本书以来已经很久了,所以我记不起当时的一切。但Pos_Validator类的特定方法按预期工作。
第一个参数是输入字段的名称。与ID必须唯一的ID不同,name属性可用于多个输入字段。要将多个字段作为数组提交到PHP脚本,请在name属性的末尾添加一对方括号。例如,以下代码将两个地址字段作为数组提交:
<p><label for="address1">Address 1:</label>
<input name="address[]" type="text" id="address1">
</p>
<p><label for="address2">Address 2:</label>
<input name="address[]" type="text" id="address2" />
</p>
如果您将Pos_Validator的实例创建为$ val,则可以通过调用removeTagsFromArray()方法从地址字段中删除标记:
$val->removeTagsFromArray('address');
这是最可能出现的情况吗?可能不是。该类的目的是演示如何使用Facade设计模式来隐藏PHP过滤器函数的复杂性。 Pos_Validator类的其他方法更实用。