4 * Validates the HTML attribute lang, effectively a language code.
5 * @note Built according to RFC 3066, which obsoleted RFC 1766
7 class HTMLPurifier_AttrDef_Lang
extends HTMLPurifier_AttrDef
11 * @param string $string
12 * @param HTMLPurifier_Config $config
13 * @param HTMLPurifier_Context $context
16 public function validate($string, $config, $context)
18 $string = trim($string);
23 $subtags = explode('-', $string);
24 $num_subtags = count($subtags);
26 if ($num_subtags == 0) { // sanity check
30 // process primary subtag : $subtags[0]
31 $length = strlen($subtags[0]);
36 if (!($subtags[0] == 'x' ||
$subtags[0] == 'i')) {
42 if (!ctype_alpha($subtags[0])) {
44 } elseif (!ctype_lower($subtags[0])) {
45 $subtags[0] = strtolower($subtags[0]);
52 $new_string = $subtags[0];
53 if ($num_subtags == 1) {
57 // process second subtag : $subtags[1]
58 $length = strlen($subtags[1]);
59 if ($length == 0 ||
($length == 1 && $subtags[1] != 'x') ||
$length > 8 ||
!ctype_alnum($subtags[1])) {
62 if (!ctype_lower($subtags[1])) {
63 $subtags[1] = strtolower($subtags[1]);
66 $new_string .= '-' . $subtags[1];
67 if ($num_subtags == 2) {
71 // process all other subtags, index 2 and up
72 for ($i = 2; $i < $num_subtags; $i++
) {
73 $length = strlen($subtags[$i]);
74 if ($length == 0 ||
$length > 8 ||
!ctype_alnum($subtags[$i])) {
77 if (!ctype_lower($subtags[$i])) {
78 $subtags[$i] = strtolower($subtags[$i]);
80 $new_string .= '-' . $subtags[$i];