10 public function validate($string, $config, $context) {
12 $string = trim($string);
13 if (!$string)
return false;
15 $subtags = explode(
'-', $string);
16 $num_subtags = count($subtags);
18 if ($num_subtags == 0)
return false;
21 $length = strlen($subtags[0]);
26 if (! ($subtags[0] ==
'x' || $subtags[0] ==
'i') ) {
32 if (! ctype_alpha($subtags[0]) ) {
34 } elseif (! ctype_lower($subtags[0]) ) {
35 $subtags[0] = strtolower($subtags[0]);
42 $new_string = $subtags[0];
43 if ($num_subtags == 1)
return $new_string;
46 $length = strlen($subtags[1]);
47 if ($length == 0 || ($length == 1 && $subtags[1] !=
'x') || $length > 8 || !ctype_alnum($subtags[1])) {
50 if (!ctype_lower($subtags[1])) $subtags[1] = strtolower($subtags[1]);
52 $new_string .=
'-' . $subtags[1];
53 if ($num_subtags == 2)
return $new_string;
56 for ($i = 2; $i < $num_subtags; $i++) {
57 $length = strlen($subtags[$i]);
58 if ($length == 0 || $length > 8 || !ctype_alnum($subtags[$i])) {
61 if (!ctype_lower($subtags[$i])) {
62 $subtags[$i] = strtolower($subtags[$i]);
64 $new_string .=
'-' . $subtags[$i];