Parses a Plural-Forms entry from a Gettext Portable Object file header.
string $pluralforms: The Plural-Forms entry value.
An array containing the number of plural forms and the converted version of the formula that can be evaluated with PHP later.
function parsePluralForms($pluralforms) {
// First, delete all whitespace.
$pluralforms = strtr($pluralforms, array(
" " => "",
"\t" => "",
));
// Select the parts that define nplurals and plural.
$nplurals = strstr($pluralforms, "nplurals=");
if (strpos($nplurals, ";")) {
// We want the string from the 10th char, because "nplurals=" length is 9.
$nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9);
}
else {
return FALSE;
}
$plural = strstr($pluralforms, "plural=");
if (strpos($plural, ";")) {
// We want the string from the 8th char, because "plural=" length is 7.
$plural = substr($plural, 7, strpos($plural, ";") - 7);
}
else {
return FALSE;
}
// Get PHP version of the plural formula.
$plural = $this
->parseArithmetic($plural);
if ($plural !== FALSE) {
return array(
$nplurals,
$plural,
);
}
else {
throw new Exception('The plural formula could not be parsed.');
}
}