php验证基于RFC-2616的标头值

时间:2014-03-04 02:10:52

标签: validation preg-match

基于RFC-2616: 我有一个UI用户输入标题名称和值。我需要验证这些值。不知何故,我想出来验证来自https://github.com/rack/rack/pull/399

的标题名称

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

以下是我的观点,但仍有任何建议值得高度赞赏

/**
 * RFC 2616 header value http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p1-messaging-22.html#rfc.section.3.2.4.p.3
 * word = token / quoted-string
 * token = 1*tchar
 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
 * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
 * / DIGIT / ALPHA
 * ; any VCHAR, except special
 * special        = "(" / ")" / "<" / ">" / "@" / ","
 * / ";" / ":" / "\" / DQUOTE / "/" / "["
 * / "]" / "?" / "=" / "{" / "}"
 */
private function isValidHeaderValue($value)
{
    $tchar = preg_replace('/[\"][@<>?={}\[\]()\"\,\;\:\/\\\\][\"]+/', '', $value);
    $remaining = preg_replace('/[a-zA-Z0-9\s\"!#$%&\'*+-.^_`|~]+/', '', $tchar);
    return ($remaining) ? false : true;
}