function _views_natural_sort_number_encode_match_callback in Views Natural Sort 6
Same name and namespace in other branches
- 7 views_natural_sort.module \_views_natural_sort_number_encode_match_callback()
Encodes a string representing numbers into a special format that can be sorted alphanumerically.
Parameters
array $match: array of matches passed from preg_replace_callback $match[0] is the entire matching string $match[1] if present, is the optional dash, preceded by optional whitespace $match[2] if present, is whole number portion of the decimal number $match[3] if present, is the fractional portion of the decimal number $match[4] if present, is the integer (when no fraction is matched)
Return value
string String representing a numerical value that will sort numerically in an alphanumeric search.
1 string reference to '_views_natural_sort_number_encode_match_callback'
- views_natural_sort_encode in ./
views_natural_sort.module - Encodes a string into an ascii-sortable such:
File
- ./
views_natural_sort.module, line 208 - Provides a views filter that sorts titles by a more natural manner by ignoring articles like "The" and "A."
Code
function _views_natural_sort_number_encode_match_callback($match) {
// Remove commas and leading zeros from whole number
$whole = (string) (int) str_replace(',', '', isset($match[4]) && strlen($match[4]) > 0 ? $match[4] : $match[2]);
// Remove traililng 0's from fraction, then add the decimal and one trailing 0
$fraction = trim('.' . $match[3], '0') . '0';
$encode = sprintf('%02u', strlen($whole)) . $whole . $fraction;
if (strlen($match[1])) {
// Negative number. Make 10's complement. Put back any leading white space and the dash
// Requires intermediate to avoid double-replacing the same digit. str_replace seems to
// work by copying the source to the result, then successively replacing within it,
// rather than replacing from the source to the result.
$digits = array(
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
);
$intermediate = array(
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
);
$rev_digits = array(
'9',
'8',
'7',
'6',
'5',
'4',
'3',
'2',
'1',
'0',
);
$encode = $match[1] . str_replace($intermediate, $rev_digits, str_replace($digits, $intermediate, $encode));
}
return $encode;
}