-// ksort() by domain
-function ksort_by_domain(& $array)
-{
- $sort = array();
- foreach(array_keys($array) as $key) {
- $sort[delimiter_reverse($key)] = $key;
- }
- ksort($sort, SORT_STRING);
-
- $result = array();
- foreach($sort as $key) {
- $result[$key] = & $array[$key];
- }
-
- $array = $result;
-}
-
-// Roughly strings(1) using PCRE
-// This function is useful to:
-// * Reduce the size of data, from removing unprintable binary data
-// * Detect _bare_strings_ from binary data
-// References:
-// http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
-// http://www.pcre.org/pcre.txt
-// Note: mb_ereg_replace() is one of mbstring extension's functions
-// and need to init its encoding.
-function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE)
-{
- // String only
- $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary);
-
- $regex = $ignore_space ?
- '[^[:graph:] \t\n]+' : // Remove "\0" etc, and readable spaces
- '[^[:graph:][:space:]]+'; // Preserve readable spaces if possible
-
- $binary = $multibyte ?
- mb_ereg_replace($regex, "\n", $binary) :
- preg_replace('/' . $regex . '/s', "\n", $binary);
-
- if ($ignore_space) {
- $binary = preg_replace(
- array(
- '/[ \t]{2,}/',
- '/^[ \t]/m',
- '/[ \t]$/m',
- ),
- array(
- ' ',
- '',
- ''
- ),
- $binary);
- }
-
- if ($min_len > 1) {
- // The last character seems "\n" or not
- $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : '';
-
- $min_len = min(1024, intval($min_len));
- $regex = '/^.{' . $min_len . ',}/S';
- $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br;
- }
-
- return $binary;
-}
-
-
-// ---------------------
-// Utilities: Arrays
-
-// Count leaves (A leaf = value that is not an array, or an empty array)
-function array_count_leaves($array = array(), $count_empty = FALSE)
-{
- if (! is_array($array) || (empty($array) && $count_empty)) return 1;
-
- // Recurse
- $count = 0;
- foreach ($array as $part) {
- $count += array_count_leaves($part, $count_empty);
- }
- return $count;
-}
-
-// Merge two leaves
-// Similar to PHP array_merge_leaves(), except strictly preserving keys as string
-function array_merge_leaves($array1, $array2, $sort_keys = TRUE)
-{
- // Array(s) only
- $is_array1 = is_array($array1);
- $is_array2 = is_array($array2);
- if ($is_array1) {
- if ($is_array2) {
- ; // Pass
- } else {
- return $array1;
- }
- } else if ($is_array2) {
- return $array2;
- } else {
- return $array2; // Not array ($array1 is overwritten)
- }
-
- $keys_all = array_merge(array_keys($array1), array_keys($array2));
- if ($sort_keys) sort($keys_all, SORT_STRING);
-
- $result = array();
- foreach($keys_all as $key) {
- $isset1 = isset($array1[$key]);
- $isset2 = isset($array2[$key]);
- if ($isset1 && $isset2) {
- // Recurse
- $result[$key] = array_merge_leaves($array1[$key], $array2[$key], $sort_keys);
- } else if ($isset1) {
- $result[$key] = & $array1[$key];
- } else {
- $result[$key] = & $array2[$key];
- }
- }
- return $result;
-}
-
-// An array-leaves to a flat array
-function array_flat_leaves($array, $unique = TRUE)
-{
- if (! is_array($array)) return $array;
-
- $tmp = array();
- foreach(array_keys($array) as $key) {
- if (is_array($array[$key])) {
- // Recurse
- foreach(array_flat_leaves($array[$key]) as $_value) {
- $tmp[] = $_value;
- }
- } else {
- $tmp[] = & $array[$key];
- }
- }