PageRenderTime 157ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/missing_functions.php

https://github.com/hthetiot/basezf
PHP | 444 lines | 260 code | 67 blank | 117 comment | 46 complexity | becad74f47247489764c10ac33e5e527 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * missing_functions.php
  4. *
  5. * @category BaseZF_Library
  6. * @package BaseZF
  7. * @copyright Copyright (c) 2008 BaseZF
  8. * @author Harold Thetiot (hthetiot)
  9. */
  10. //
  11. // Example Function
  12. //
  13. if (!function_exists('example')) {
  14. /**
  15. * This is a example function
  16. *
  17. * @param string $string a little string
  18. * @param string $array a little array
  19. * @return array $array value
  20. */
  21. function example($string, array $array = array())
  22. {
  23. return $array;
  24. }
  25. }
  26. if (!function_exists('define_if_not')) {
  27. /**
  28. * Define a constant only if not defined
  29. *
  30. * @param string $constant Constant name
  31. * @param void $value Constant value
  32. * @return defined with success return true then false
  33. */
  34. function define_if_not($constant, $value)
  35. {
  36. return defined($constant) or define($constant, $value);
  37. }
  38. }
  39. if (!function_exists('__')) {
  40. /**
  41. * Handler around _() gettext function
  42. *
  43. * @param string $string no transalted string
  44. * @return string $string transalted string
  45. */
  46. function __($string, $domain = null)
  47. {
  48. if (!empty($domain)) {
  49. return dgettext($domain, $string);
  50. } else {
  51. return _($string);
  52. }
  53. }
  54. }
  55. if (!function_exists('bytes_to_human_size')) {
  56. /**
  57. * Convert bytes to human readable size
  58. *
  59. * @param int $size
  60. * @param int $decimals
  61. * @return string
  62. */
  63. function bytes_to_human_size($size, $decimals = 1)
  64. {
  65. $suffix = array('Bytes','KB','MB','GB','TB','PB','EB','ZB','YB','NB','DB');
  66. $i = 0;
  67. while ($size >= 1024 && ($i < count($suffix) - 1)) {
  68. $size /= 1024;
  69. $i++;
  70. }
  71. return round($size, $decimals) . ' ' . $suffix[$i];
  72. }
  73. }
  74. if (!function_exists('array_set_current')) {
  75. /**
  76. * Set current position of array
  77. *
  78. * @param array $array to update position by reference
  79. * @param void $key requested position of array
  80. * @return void new array potision
  81. */
  82. function array_set_current(array &$array, $key)
  83. {
  84. reset($array);
  85. while (current($array) !== false) {
  86. if (key($array) == $key) {
  87. break;
  88. }
  89. next($array);
  90. }
  91. return current($array);
  92. }
  93. }
  94. if (!function_exists('uasort_by_length')) {
  95. function uasort_by_length_callback($a,$b)
  96. {
  97. // return isset($b[strlen($a) - 1]); // Jviveret say it more fast
  98. return strlen($b)-strlen($a);
  99. }
  100. function uasort_by_length(array $array, $revert = false)
  101. {
  102. uasort($array, 'uasort_by_length_callback');
  103. if ($revert) {
  104. return array_reverse($array, true);
  105. }
  106. return $array;
  107. }
  108. }
  109. if (!function_exists('uksort_by_length')) {
  110. function uksort_by_length_callback($a,$b)
  111. {
  112. // return isset($b[strlen($a) - 1]); // Jviveret say it more fast
  113. return strlen($b)-strlen($a);
  114. }
  115. function uksort_by_length(array $array, $revert = false)
  116. {
  117. uksort($array, 'uksort_by_length_callback');
  118. if ($revert) {
  119. return array_reverse($array, true);
  120. }
  121. return $array;
  122. }
  123. }
  124. if (!function_exists('hash_element_by_deep')) {
  125. /**
  126. * Return a directory where element_id is store
  127. *
  128. * @param unknown_type $element_id
  129. * @param unknown_type $deep_dir
  130. * @param unknown_type $deep_dirs
  131. * @return unknown
  132. */
  133. function hash_element_by_deep($element_id, $deep_dir, $deep_dirs)
  134. {
  135. if (strlen($element_id) > ($deep_dir * ($deep_dirs + 1))) {
  136. return false;
  137. }
  138. return wordwrap( str_pad( floor( $element_id / pow(10, $deep_dir) ), $deep_dir * $deep_dirs, '0', STR_PAD_LEFT ), $deep_dir, '/', true );
  139. }
  140. }
  141. if (!function_exists('mb_ucfirst')) {
  142. /**
  143. * @todo doc
  144. */
  145. function mb_ucfirst($string, $encoding = 'UTF-8')
  146. {
  147. if ($encoding) {
  148. return mb_strtoupper(mb_substr($string, 0, 1, $encoding), $encoding) . mb_substr($string, 1, mb_strlen($string, $encoding) - 1, $encoding);
  149. } else {
  150. return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1, mb_strlen($string) - 1);
  151. }
  152. }
  153. }
  154. if (!function_exists('wordlimit')) {
  155. /**
  156. * @todo doc
  157. */
  158. function wordlimit($string, $length = 50, $ellipsis = "...")
  159. {
  160. $words = explode(' ', $string);
  161. if (count($words) > $length)
  162. return implode(' ', array_slice($words, 0, $length)) . $ellipsis;
  163. else
  164. return $string;
  165. }
  166. }
  167. if (!function_exists('wordlimit_bychar')) {
  168. /**
  169. * @todo doc
  170. */
  171. function wordlimit_bychar($string, $length = 50, $ellipsis = "...")
  172. {
  173. if (mb_strlen($string) < $length) {
  174. return $string;
  175. }
  176. return substr($string, 0, $length) . $ellipsis;
  177. }
  178. }
  179. if (!function_exists('parseCSS')) {
  180. /**
  181. * @todo doc
  182. */
  183. function parseCSS($filename)
  184. {
  185. $fp = fopen($filename, "r");
  186. $css = fread($fp, filesize ($filename));
  187. fclose($fp);
  188. $css = preg_replace("/[\s,]+/", "", $css);
  189. $css_class = preg_split("/}/", $css);
  190. while (list($key,$val) = each ($css_class)) {
  191. $aCSSObj = preg_split("/{/", $val);
  192. if (!isset($aCSSObj[1])) {
  193. continue;
  194. }
  195. $a = preg_split("/;/", $aCSSObj[1]);
  196. while(list($key,$val0) = each ($a)) {
  197. if ($val0 !='') {
  198. $aCSSSub = preg_split("/:/", $val0);
  199. $aCSSItem[$aCSSSub[0]]=$aCSSSub[1];
  200. }
  201. }
  202. $aCSS[$aCSSObj[0]]=$aCSSItem;
  203. unset($aCSSItem);
  204. }
  205. return $aCSS;
  206. }
  207. }
  208. if (!function_exists('count_days')) {
  209. // Will return the number of days between the two dates passed in
  210. /**
  211. * @todo doc
  212. */
  213. function count_days( $a, $b )
  214. {
  215. // First we need to break these dates into their constituent parts:
  216. $gd_a = getdate( $a );
  217. $gd_b = getdate( $b );
  218. // Now recreate these timestamps, based upon noon on each day
  219. // The specific time doesn't matter but it must be the same each day
  220. $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
  221. $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
  222. // Subtract these two numbers and divide by the number of seconds in a
  223. // day. Round the result since crossing over a daylight savings time
  224. // barrier will cause this time to be off by an hour or two.
  225. return round( abs( $a_new - $b_new ) / 86400 );
  226. }
  227. }
  228. if (!function_exists('stringToAscii')) {
  229. /**
  230. * Convert a string to ascii
  231. *
  232. * @param string $string a little string
  233. * @return array $array value
  234. */
  235. function stringToAscii($string)
  236. {
  237. $transliteration = array(
  238. "À" => "A","Á" => "A","Â" => "A","Ã" => "A","Ä" => "A",
  239. "Å" => "A","Æ" => "A","Ā" => "A","Ą" => "A","Ă" => "A",
  240. "Ç" => "C","Ć" => "C","Č" => "C","Ĉ" => "C","Ċ" => "C",
  241. "Ď" => "D","Đ" => "D","È" => "E","É" => "E","Ê" => "E",
  242. "Ë" => "E","Ē" => "E","Ę" => "E","Ě" => "E","Ĕ" => "E",
  243. "Ė" => "E","Ĝ" => "G","Ğ" => "G","Ġ" => "G","Ģ" => "G",
  244. "Ĥ" => "H","Ħ" => "H","Ì" => "I","Í" => "I","Î" => "I",
  245. "Ï" => "I","Ī" => "I","Ĩ" => "I","Ĭ" => "I","Į" => "I",
  246. "İ" => "I","IJ" => "IJ","Ĵ" => "J","Ķ" => "K","Ľ" => "K",
  247. "Ĺ" => "K","Ļ" => "K","Ŀ" => "K","Ł" => "L","Ñ" => "N",
  248. "Ń" => "N","Ň" => "N","Ņ" => "N","Ŋ" => "N","Ò" => "O",
  249. "Ó" => "O","Ô" => "O","Õ" => "O","Ö" => "Oe","Ø" => "O",
  250. "Ō" => "O","Ő" => "O","Ŏ" => "O","Œ" => "OE","Ŕ" => "R",
  251. "Ř" => "R","Ŗ" => "R","Ś" => "S","Ş" => "S","Ŝ" => "S",
  252. "Ș" => "S","Š" => "S","Ť" => "T","Ţ" => "T","Ŧ" => "T",
  253. "Ț" => "T","Ù" => "U","Ú" => "U","Û" => "U","Ü" => "Ue",
  254. "Ū" => "U","Ů" => "U","Ű" => "U","Ŭ" => "U","Ũ" => "U",
  255. "Ų" => "U","Ŵ" => "W","Ŷ" => "Y","Ÿ" => "Y","Ý" => "Y",
  256. "Ź" => "Z","Ż" => "Z","Ž" => "Z","à" => "a","á" => "a",
  257. "â" => "a","ã" => "a","ä" => "ae","ā" => "a","ą" => "a",
  258. "ă" => "a","å" => "a","æ" => "ae","ç" => "c","ć" => "c",
  259. "č" => "c","ĉ" => "c","ċ" => "c","ď" => "d","đ" => "d",
  260. "è" => "e","é" => "e","ê" => "e","ë" => "e","ē" => "e",
  261. "ę" => "e","ě" => "e","ĕ" => "e","ė" => "e","ƒ" => "f",
  262. "ĝ" => "g","ğ" => "g","ġ" => "g","ģ" => "g","ĥ" => "h",
  263. "ħ" => "h","ì" => "i","í" => "i","î" => "i","ï" => "i",
  264. "ī" => "i","ĩ" => "i","ĭ" => "i","į" => "i","ı" => "i",
  265. "ij" => "ij","ĵ" => "j","ķ" => "k","ĸ" => "k","ł" => "l",
  266. "ľ" => "l","ĺ" => "l","ļ" => "l","ŀ" => "l","ñ" => "n",
  267. "ń" => "n","ň" => "n","ņ" => "n","ʼn" => "n","ŋ" => "n",
  268. "ò" => "o","ó" => "o","ô" => "o","õ" => "o","ö" => "oe",
  269. "ø" => "o","ō" => "o","ő" => "o","ŏ" => "o","œ" => "oe",
  270. "ŕ" => "r","ř" => "r","ŗ" => "r","ś" => "s","š" => "s",
  271. "ť" => "t","ù" => "u","ú" => "u","û" => "u","ü" => "ue",
  272. "ū" => "u","ů" => "u","ű" => "u","ŭ" => "u","ũ" => "u",
  273. "ų" => "u","ŵ" => "w","ÿ" => "y","ý" => "y","ŷ" => "y",
  274. "ż" => "z","ź" => "z","ž" => "z","ß" => "ss","ſ" => "ss");
  275. return str_replace( array_keys( $transliteration ), array_values( $transliteration ), $string);
  276. }
  277. }
  278. if (!function_exists('implode_assoc')) {
  279. /**
  280. * Implodes a single dimensional associative array with various formatting options / modifiers.
  281. *
  282. * @param array $array single dimensional array to implode
  283. * @param array $overrideOptions is an key->value array with the following valid values:
  284. * - inner_glue => string to connect keys to values with
  285. * - outer_glue => string to connect keys-value pairs together
  286. * - prepend => string to attach to the front of the final result
  287. * - append => string to attach to the end of the final result
  288. * - skip_empty => bool if true then do not include entries with values that evaluate to false
  289. * - prepend_inner_glue => bool if true then stick the inner_glue on to the front of all key-value pairs
  290. * - append_inner_glue => bool if true then stick the inner_glue on to the end of all key-value pairs
  291. * - prepend_outer_glue => bool if true then stick the outer_glue on to the front of the return string
  292. * - append_outer_glue => bool if true then stick the outer_glue on to the end of the return string
  293. * - urlencode => bool if true then urlencode() all returned values
  294. * - part => string setting what part(s) of the key-value pairs to return; valid values:
  295. * - both -> display both the key and the value
  296. * - key -> display the key and NOT the value; inner_glue will not display except with prepend/append
  297. * - value -> display the value and NOT the key; inner_glue will not display except with prepend/append
  298. *
  299. * @return string of the imploded key-value pairs
  300. */
  301. function implode_assoc($array, array $overrideOptions = array())
  302. {
  303. // These default options set the defaults but are over-written by matching values from $overrideOptions
  304. $options = array(
  305. 'inner_glue' => '=',
  306. 'outer_glue' => '&',
  307. 'prepend' => '',
  308. 'append' => '',
  309. 'skip_empty' => false,
  310. 'prepend_inner_glue' => false,
  311. 'append_inner_glue' => false,
  312. 'prepend_outer_glue' => false,
  313. 'append_outer_glue' => false,
  314. 'urlencode' => false,
  315. 'part' => 'both' //'both', 'key', or 'value'
  316. );
  317. // Use values from $overrideOptions that match keys in $options and then extract those values into
  318. // the current workspace.
  319. foreach ($overrideOptions as $key=>$val) {
  320. if (isset($options[$key])) {
  321. $options[$key] = $val;
  322. }
  323. }
  324. extract($options);
  325. // $output holds the imploded results of the key-value pairs
  326. $output = array();
  327. // Create a collection of the inner key-value pairs and glue them as indicated by the $options
  328. foreach ($array as $key=>$item) {
  329. // If not skipping empty values OR if the item evaluates to true.
  330. // i.e. If $skip_empty is true then check to see if the array item's value evaluates to true.
  331. if (!$skip_empty || $item) {
  332. $output[] =
  333. ($prepend_inner_glue ? $inner_glue : '').
  334. ($part != 'value' ? $key : ''). // i.e. show the $key if $part is 'both' or 'key'
  335. ($part == 'both' ? $inner_glue : '').
  336. // i.e. show the $item if $part is 'both' or 'value' and optionally urlencode $item
  337. ($part != 'key' ? ($urlencode ? urlencode($item) : $item) : '').
  338. ($append_inner_glue ? $inner_glue : '');
  339. }
  340. }
  341. return $prepend. ($prepend_outer_glue ? $outer_glue : '') . implode($outer_glue, $output) . ($append_outer_glue ? $outer_glue : '') . $append;
  342. }
  343. }
  344. if (!function_exists('common_words')) {
  345. /**
  346. * Extract common word from string and exclude stop words
  347. *
  348. */
  349. function common_words($string, $stopWords)
  350. {
  351. $string = preg_replace('/ss+/i', '', $string);
  352. $string = trim($string); // trim the string
  353. $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too...
  354. $string = strtolower($string); // make it lowercase
  355. preg_match_all('/([a-z]*?)(?=s)/i', $string, $matchWords);
  356. $matchWords = $matchWords[0];
  357. foreach ( $matchWords as $key=>$item ) {
  358. if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
  359. unset($matchWords[$key]);
  360. }
  361. }
  362. $wordCountArr = array();
  363. if ( is_array($matchWords) ) {
  364. foreach ( $matchWords as $key => $val ) {
  365. $val = strtolower($val);
  366. if ( isset($wordCountArr[$val]) ) {
  367. $wordCountArr[$val]++;
  368. } else {
  369. $wordCountArr[$val] = 1;
  370. }
  371. }
  372. }
  373. arsort($wordCountArr);
  374. $wordCountArr = array_slice($wordCountArr, 0, 10);
  375. return $wordCountArr;
  376. }
  377. }