PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/php/string/string_utilities.class.php

https://bitbucket.org/chamilo/chamilo/
PHP | 312 lines | 202 code | 27 blank | 83 comment | 24 complexity | 24646eba24fb4eb5f8620c4f73be5842 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. namespace common\libraries;
  3. use Exception;
  4. /**
  5. * $Id: string.class.php 128 2009-11-09 13:13:20Z vanpouckesven $
  6. * @package common.string
  7. */
  8. class StringUtilities
  9. {
  10. const NEW_LINE = "\n";
  11. /**
  12. * Tests if a string starts with a given string
  13. *
  14. * @param string $string
  15. * @param string $start
  16. * @return bool
  17. */
  18. public static function start_with($string, $start, $case_sensitive = true)
  19. {
  20. if ($case_sensitive)
  21. {
  22. return strpos($string, $start) === 0;
  23. }
  24. else
  25. {
  26. return stripos($string, $start) === 0;
  27. }
  28. }
  29. /**
  30. * Tests if a string ends with the given string
  31. *
  32. * @param string $string
  33. * @param string $end
  34. * @param bool $case_sensitive
  35. * @return bool
  36. */
  37. public static function end_with($string, $end, $case_sensitive = true)
  38. {
  39. if ($case_sensitive)
  40. {
  41. return strrpos($string, $end) === strlen($string) - strlen($end);
  42. }
  43. else
  44. {
  45. return strripos($string, $end) === strlen($string) - strlen($end);
  46. }
  47. }
  48. /**
  49. * Indicates wether the given string has some value (meaning neither null nor empty)
  50. * @param string $string
  51. * @param boolean $for_humans
  52. * @return boolean
  53. */
  54. public static function has_value($string, $for_humans = false)
  55. {
  56. return ! StringUtilities :: is_null_or_empty($string, $for_humans);
  57. }
  58. /**
  59. * Indicates wether the given string is null or empty
  60. * @param string $string
  61. * @param boolean $for_humans
  62. * @return boolean
  63. */
  64. public static function is_null_or_empty($string, $for_humans = false)
  65. {
  66. if ($for_humans)
  67. {
  68. $string = trim(self :: strip_only_tags($string));
  69. }
  70. if (isset($string))
  71. {
  72. if (is_string($string))
  73. {
  74. if (strlen($string) == 0)
  75. {
  76. return true;
  77. }
  78. else
  79. {
  80. return false;
  81. }
  82. }
  83. else
  84. {
  85. return false;
  86. }
  87. }
  88. else
  89. {
  90. return true;
  91. }
  92. }
  93. /**
  94. * Ensure a string starts with another given string
  95. *
  96. * @param $string The string that must start with a leading string
  97. * @param $leading_string The string to add at the beginning of the main string if necessary
  98. * @return string
  99. */
  100. public static function ensure_start_with($string, $leading_string)
  101. {
  102. if (StringUtilities :: start_with($string, $leading_string))
  103. {
  104. return $string;
  105. }
  106. else
  107. {
  108. return $leading_string . $string;
  109. }
  110. }
  111. /**
  112. * Remove a trailing string from a string if it exists
  113. * @param $string The string that must be shortened if it ends with a trailing string
  114. * @param $trailing_string The trailing string
  115. * @return string
  116. */
  117. public static function remove_trailing($string, $trailing_string)
  118. {
  119. if (StringUtilities :: end_with($string, $trailing_string))
  120. {
  121. return substr($string, 0, strlen($string) - strlen($trailing_string));
  122. }
  123. else
  124. {
  125. return $string;
  126. }
  127. }
  128. /**
  129. * Return the string found between two characters.
  130. *
  131. * If an index is given, it returns the value at the index position.
  132. * e.g. $index = 3 --> returns the value between the third occurence of $opening_char and $closing_char
  133. *
  134. * @param string $opening_char
  135. * @param string $closing_char
  136. * @param int $index 0 based index
  137. * @return string or null
  138. */
  139. public static function get_value_between_chars($haystack, $index = 0, $opening_char = '[', $closing_char = ']')
  140. {
  141. $offset = 0;
  142. $found = true;
  143. $value = null;
  144. for($i = 0; $i < $index + 1; $i ++)
  145. {
  146. $op_pos = strpos($haystack, $opening_char, $offset);
  147. if ($op_pos !== false)
  148. {
  149. $cl_pos = strpos($haystack, $closing_char, $op_pos + 1);
  150. if ($cl_pos !== false)
  151. {
  152. $value = substr($haystack, $op_pos + 1, $cl_pos - $op_pos - 1);
  153. $offset = $cl_pos + 1;
  154. }
  155. else
  156. {
  157. $found = false;
  158. break;
  159. }
  160. }
  161. else
  162. {
  163. $found = false;
  164. break;
  165. }
  166. }
  167. if ($found)
  168. {
  169. return $value;
  170. }
  171. else
  172. {
  173. return null;
  174. }
  175. }
  176. /**
  177. * Build an array from a list of strings
  178. *
  179. * E.g: Array with the following strings:
  180. *
  181. * 'general_description[0][0][string]'
  182. * 'general_description[0][1][string]'
  183. * 'general_description[1][0][string]'
  184. *
  185. * @param array $strings Array of (strings => value) pairs to merge into a multilevel array
  186. * @param string $opening_char
  187. * @param string $closing_char
  188. */
  189. public static function to_multilevel_array($strings, $opening_char = '[', $closing_char = ']')
  190. {
  191. $array = array();
  192. foreach ($strings as $string => $value)
  193. {
  194. StringUtilities :: set_next_level_array($array, $string, $value, $opening_char, $closing_char);
  195. }
  196. return $array;
  197. }
  198. private static function set_next_level_array(&$container_array, $string, $value, $opening_char = '[', $closing_char = ']')
  199. {
  200. $key = StringUtilities :: get_value_between_chars($string, 0, $opening_char, $closing_char);
  201. $sub_string = substr($string, strpos($string, $closing_char) + 1);
  202. if (isset($sub_string) && strlen($sub_string) > 0)
  203. {
  204. if (isset($container_array[$key]))
  205. {
  206. $sub_array = $container_array[$key];
  207. }
  208. else
  209. {
  210. $sub_array = array();
  211. }
  212. StringUtilities :: set_next_level_array($sub_array, $sub_string, $value, $opening_char, $closing_char);
  213. $container_array[$key] = $sub_array;
  214. }
  215. else
  216. {
  217. if (isset($container_array[$key]))
  218. {
  219. $container_array[$key] = array_merge($container_array[$key], $value);
  220. }
  221. else
  222. {
  223. $container_array[$key] = $value;
  224. }
  225. }
  226. }
  227. /**
  228. * Escape a string according to the mysql way of escaping.
  229. *
  230. * This function should only be used when it is not possible to use the mysql_real_escape_string()
  231. * because you don't have any open connection to the database yet
  232. *
  233. * @param string $string_to_escape
  234. * @return string
  235. */
  236. public static function escape_mysql($string_to_escape, $quote_char = "'")
  237. {
  238. if ($quote_char != '"' && $quote_char != "'")
  239. {
  240. throw new Exception('Unvalid quote char for MySQL query');
  241. }
  242. $string_to_escape = str_ireplace("\\", "\\\\", $string_to_escape);
  243. if ($quote_char == "'")
  244. {
  245. $string_to_escape = str_ireplace("'", "\'", $string_to_escape);
  246. }
  247. elseif ($quote_char == '"')
  248. {
  249. $string_to_escape = str_ireplace('"', '\"', $string_to_escape);
  250. }
  251. $string_to_escape = str_ireplace("\n", '\n', $string_to_escape);
  252. $string_to_escape = str_ireplace("\r", '\r', $string_to_escape);
  253. $string_to_escape = str_ireplace("\x00", '\\\\x00', $string_to_escape);
  254. $string_to_escape = str_ireplace("\x1a", '\\\\x1a', $string_to_escape);
  255. return $string_to_escape;
  256. }
  257. /**
  258. * @param string $string
  259. * @param multitype $tags
  260. * @param boolean $strip_content
  261. * @return mixed
  262. */
  263. static function strip_only_tags($string, $tags = array('br', 'p', 'div', 'span'), $strip_content = false)
  264. {
  265. $content = '';
  266. if (! is_array($tags))
  267. {
  268. $tags = (strpos($string, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
  269. if (end($tags) == '')
  270. {
  271. array_pop($tags);
  272. }
  273. }
  274. foreach ($tags as $tag)
  275. {
  276. if ($strip_content)
  277. {
  278. $content = '(.+</' . $tag . '(>|\s[^>]*>)|)';
  279. }
  280. $string = preg_replace('#</?' . $tag . '(>|\s[^>]*>)' . $content . '#is', '', $string);
  281. }
  282. return $string;
  283. }
  284. }
  285. ?>