2 // $Id: spam_util.php,v 1.2 2009/01/04 08:56:07 henoheno Exp $
3 // Copyright (C) 2006-2009 PukiWiki Developers Team
4 // License: GPL v2 or (at your option) any later version
6 // Functions for Concept-work of spam-uri metrics
9 // ---------------------
12 // (PHP 4 >= 4.2.0): var_export(): mail-reporting and dump related
13 if (! function_exists('var_export')) {
14 function var_export() {
15 return 'var_export() is not found on this server' . "\n";
19 // (PHP 4 >= 4.2.0): preg_grep() enables invert option
20 function preg_grep_invert($pattern = '//', $input = array())
23 if (! isset($invert)) $invert = defined('PREG_GREP_INVERT');
26 return preg_grep($pattern, $input, PREG_GREP_INVERT);
28 $result = preg_grep($pattern, $input);
30 return array_diff($input, preg_grep($pattern, $input));
38 // ---------------------
41 // Very roughly, shrink the lines of var_export()
42 // NOTE: If the same data exists, it must be corrupted.
43 function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
45 $result = var_export($expression, TRUE);
47 $result = preg_replace(
48 // Remove a newline and spaces
49 '# => \n *array \(#', ' => array (',
53 if ($ignore_numeric_keys) {
54 $result =preg_replace(
55 // Remove numeric keys
56 '#^( *)[0-9]+ => #m', '$1',
69 // Data structure: Create an array they _refer_only_one_ value
70 function one_value_array($num = 0, $value = NULL)
72 $num = max(0, intval($num));
75 for ($i = 0; $i < $num; $i++) {
82 // Reverse $string with specified delimiter
83 function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = NULL)
85 $to_null = ($to_delim === NULL);
87 if (! is_string($from_delim) || (! $to_null && ! is_string($to_delim))) {
90 if (is_array($string)) {
92 $count = count($string);
93 $from = one_value_array($count, $from_delim);
95 // Note: array_map() vanishes all keys
96 return array_map('delimiter_reverse', $string, $from);
98 $to = one_value_array($count, $to_delim);
99 // Note: array_map() vanishes all keys
100 return array_map('delimiter_reverse', $string, $from, $to);
103 if (! is_string($string)) {
107 // Returns com.example.bar.foo
108 if ($to_null) $to_delim = & $from_delim;
109 return implode($to_delim, array_reverse(explode($from_delim, $string)));
113 function ksort_by_domain(& $array)
116 foreach(array_keys($array) as $key) {
117 $reversed = delimiter_reverse($key);
118 if ($reversed !== FALSE) {
119 $sort[$reversed] = $key;
122 ksort($sort, SORT_STRING);
125 foreach($sort as $key) {
126 $result[$key] = & $array[$key];
132 // Roughly strings(1) using PCRE
133 // This function is useful to:
134 // * Reduce the size of data, from removing unprintable binary data
135 // * Detect _bare_strings_ from binary data
137 // http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
138 // http://www.pcre.org/pcre.txt
139 // Note: mb_ereg_replace() is one of mbstring extension's functions
140 // and need to init its encoding.
141 function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE)
144 $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary);
146 $regex = $ignore_space ?
147 '[^[:graph:] \t\n]+' : // Remove "\0" etc, and readable spaces
148 '[^[:graph:][:space:]]+'; // Preserve readable spaces if possible
150 $binary = $multibyte ?
151 mb_ereg_replace($regex, "\n", $binary) :
152 preg_replace('/' . $regex . '/s', "\n", $binary);
155 $binary = preg_replace(
170 // The last character seems "\n" or not
171 $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : '';
173 $min_len = min(1024, intval($min_len));
174 $regex = '/^.{' . $min_len . ',}/S';
175 $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br;
182 // ---------------------
185 // Count leaves (A leaf = value that is not an array, or an empty array)
186 function array_count_leaves($array = array(), $count_empty = FALSE)
188 if (! is_array($array) || (empty($array) && $count_empty)) return 1;
192 foreach ($array as $part) {
193 $count += array_count_leaves($part, $count_empty);
199 // Similar to PHP array_merge_leaves(), except strictly preserving keys as string
200 function array_merge_leaves($array1, $array2, $sort_keys = TRUE)
203 $is_array1 = is_array($array1);
204 $is_array2 = is_array($array2);
211 } else if ($is_array2) {
214 return $array2; // Not array ($array1 is overwritten)
217 $keys_all = array_merge(array_keys($array1), array_keys($array2));
218 if ($sort_keys) sort($keys_all, SORT_STRING);
221 foreach($keys_all as $key) {
222 $isset1 = isset($array1[$key]);
223 $isset2 = isset($array2[$key]);
224 if ($isset1 && $isset2) {
226 $result[$key] = array_merge_leaves($array1[$key], $array2[$key], $sort_keys);
227 } else if ($isset1) {
228 $result[$key] = & $array1[$key];
230 $result[$key] = & $array2[$key];
236 // An array-leaves to a flat array
237 function array_flat_leaves($array, $unique = TRUE)
239 if (! is_array($array)) return $array;
242 foreach(array_keys($array) as $key) {
243 if (is_array($array[$key])) {
245 foreach(array_flat_leaves($array[$key]) as $_value) {
249 $tmp[] = & $array[$key];
253 return $unique ? array_values(array_unique($tmp)) : $tmp;
256 // $array['something'] => $array['wanted']
257 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
259 if (! is_array($array) || ! is_array($keys)) return FALSE;
261 // Nondestructive test
263 foreach(array_keys($keys) as $from) {
264 if (! isset($array[$from])) {
270 foreach($keys as $from => $to) {
271 if ($from === $to) continue;
272 if (! $force || isset($array[$from])) {
273 $array[$to] = & $array[$from];
274 unset($array[$from]);
276 $array[$to] = $default;
283 // Remove redundant values from array()
284 function array_unique_recursive($array = array())
286 if (! is_array($array)) return $array;
289 foreach($array as $key => $value){
290 if (is_array($value)) {
291 $array[$key] = array_unique_recursive($value);
293 if (isset($tmp[$value])) {