Util.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * A set of static utility functions.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Util
  24. {
  25. const STATES_LONG = 1;
  26. const STATES_SHORT = 2;
  27. const STATES_BOTH = 3;
  28. public static function StatesArray($type = self::STATES_BOTH)
  29. {
  30. $short = array('AL','AK','AZ','AR','CA','CO','CT','DE','DC','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN',
  31. 'MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV',
  32. 'WI','WY');
  33. $long = array('Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','District of Columbia','Florida',
  34. 'Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts',
  35. 'Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico',
  36. 'New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina',
  37. 'South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming');
  38. switch($type)
  39. {
  40. case self::STATES_LONG:
  41. return $long;
  42. break;
  43. case self::STATES_SHORT:
  44. sort($short);
  45. return $short;
  46. break;
  47. default:
  48. return array_combine($short, $long);
  49. }
  50. }
  51. /**
  52. * Grabs and returns only the first word of the sentence. Sometimes that's all you need.
  53. * @param string $sentence
  54. */
  55. public static function FirstWord($sentence)
  56. {
  57. return substr($sentence, 0, strpos($sentence, ' '));
  58. }
  59. /**
  60. * A simple function that is useful for limiting a string to a specified number of words.
  61. * @param string $sentence
  62. * @param int $limit
  63. * @return string
  64. */
  65. public static function WordLimit($sentence, $limit)
  66. {
  67. $words = explode(' ', trim($sentence));
  68. $phrase = '';
  69. if(count($words) < $limit)
  70. {
  71. $limit = count($words);
  72. }
  73. for($i=0; $i<$limit; $i++)
  74. {
  75. $phrase .= $words[$i].' ';
  76. }
  77. return trim($phrase);
  78. }
  79. /**
  80. * A simple function that is useful for counting the number of words in a string.
  81. * @param string $sentence
  82. * @param int $limit
  83. * @return int
  84. */
  85. public static function WordCount($sentence)
  86. {
  87. return count(explode(' ', trim($sentence)));
  88. }
  89. /**
  90. * Multi-dimensional recursive array search function.
  91. * @param mixed $needle
  92. * @param array $haystack
  93. */
  94. public static function ArraySearch($needle, array $haystack)
  95. {
  96. foreach($haystack as $key=>$value)
  97. {
  98. if(is_array($value) && !is_array($needle))
  99. {
  100. if(($res = self::ArraySearch($needle, $value)) !== false)
  101. {
  102. return array_merge(array($key), (array)$res);
  103. }
  104. }
  105. else
  106. {
  107. if ($value == $needle)
  108. {
  109. return array($key);
  110. }
  111. }
  112. }
  113. return false;
  114. }
  115. }
  116. ?>