PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Digitalus/Toolbox/String.php

http://digitalus-cms.googlecode.com/
PHP | 216 lines | 103 code | 20 blank | 93 comment | 8 complexity | 8315be607f52ba0a496389b45488b669 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Digitalus CMS
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://digitalus-media.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@digitalus-media.com so we can send you a copy immediately.
  14. *
  15. * @author Forresst Lyman
  16. * @category Digitalus CMS
  17. * @package Digitalus
  18. * @subpackage Digitalus_Toolbox
  19. * @copyright Copyright (c) 2007 - 2010, Digitalus Media USA (digitalus-media.com)
  20. * @license http://digitalus-media.com/license/new-bsd New BSD License
  21. * @version $Id: String.php 767 2010-05-04 19:22:01Z lowtower@gmx.de $
  22. */
  23. class Digitalus_Toolbox_String
  24. {
  25. const UNDERSCORE = '[US]';
  26. /**
  27. * returns a randomly generated string
  28. * commonly used for password generation
  29. *
  30. * @param int $length
  31. * @return string
  32. */
  33. public static function random($length = 8)
  34. {
  35. // start with a blank string
  36. $string = '';
  37. // define possible characters
  38. $possible = '0123456789abcdfghjkmnpqrstvwxyz';
  39. // set up a counter
  40. $i = 0;
  41. // add random characters to $string until $length is reached
  42. while ($i < $length) {
  43. // pick a random character from the possible ones
  44. $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  45. // we don't want this character if it's already in the string
  46. if (!strstr($string, $char)) {
  47. $string .= $char;
  48. $i++;
  49. }
  50. }
  51. // done!
  52. return $string;
  53. }
  54. /**
  55. * replaces spaces with hyphens (used for urls)
  56. *
  57. * @param string $string
  58. * @return string
  59. */
  60. public static function addHyphens($string)
  61. {
  62. return str_replace(' ', '-', trim($string));
  63. }
  64. /**
  65. * replaces hypens with spaces
  66. *
  67. * @param string $string
  68. * @return string
  69. */
  70. public static function stripHyphens($string)
  71. {
  72. return str_replace('-', ' ', trim($string));
  73. }
  74. /**
  75. * replace empty spaces with underscores
  76. *
  77. * @param string $string
  78. * @return string
  79. */
  80. public static function replaceEmptySpace($string)
  81. {
  82. return str_replace(' ', '_', trim($string));
  83. }
  84. /**
  85. * replace underscores with empty spaces
  86. *
  87. * @param string $string
  88. * @return string
  89. */
  90. public static function replaceUnderscore($string)
  91. {
  92. return str_replace('_', ' ', trim($string));
  93. }
  94. /**
  95. * replace slashes with underscores
  96. *
  97. * @param string $string
  98. * @return string
  99. */
  100. public static function addUnderscores($string)
  101. {
  102. $string = str_replace('_', self::UNDERSCORE, $string);
  103. return str_replace('/', '_', trim($string));
  104. }
  105. /**
  106. * replaces underscores with slashes
  107. * if relative is true then return the path as relative
  108. *
  109. * @param string $string
  110. * @param bool $relative
  111. * @return string
  112. */
  113. public static function stripUnderscores($string, $relative = false)
  114. {
  115. $string = str_replace('_', '/', trim($string));
  116. if ($relative) {
  117. $string = Digitalus_Toolbox_String::stripLeading('/', $string);
  118. }
  119. $string = str_replace(self::UNDERSCORE, '_', $string);
  120. return $string;
  121. }
  122. /**
  123. * strips the leading $replace from the $string
  124. *
  125. * @param string $replace
  126. * @param string $string
  127. * @return string
  128. */
  129. public static function stripLeading($replace, $string)
  130. {
  131. if (substr($string, 0, strlen($replace)) == $replace) {
  132. return substr($string, strlen($replace));
  133. }
  134. return $string;
  135. }
  136. /**
  137. * returns the parent from the passed path
  138. *
  139. * @param string $path
  140. * @return string
  141. */
  142. public static function getParentFromPath($path)
  143. {
  144. $path = Digitalus_Toolbox_Regex::stripTrailingSlash($path);
  145. $parts = explode('/', $path);
  146. array_pop($parts);
  147. return implode('/', $parts);
  148. }
  149. /**
  150. * returns the current file from the path
  151. * this is a custom version of basename
  152. *
  153. * @param string $path
  154. * @return string
  155. */
  156. public static function getSelfFromPath($path)
  157. {
  158. $path = Digitalus_Toolbox_Regex::stripTrailingSlash($path);
  159. $parts = explode('/', $path);
  160. return array_pop($parts);
  161. }
  162. public static function truncateText($text, $count = 25, $stripTags = true)
  163. {
  164. if ($stripTags) {
  165. $filter = new Zend_Filter_StripTags();
  166. $text = $filter->filter($text);
  167. }
  168. $words = split(' ', $text);
  169. $text = (string)join(' ', array_slice($words, 0, $count));
  170. return $text;
  171. }
  172. public static function booleanise($string)
  173. {
  174. $string = strtolower($string);
  175. switch ($string) {
  176. case 'true':
  177. return true;
  178. break;
  179. case 'false':
  180. return false;
  181. break;
  182. default:
  183. $int = (int)$string;
  184. switch ($int) {
  185. case 1:
  186. return true;
  187. break;
  188. case 0:
  189. return false;
  190. break;
  191. default:
  192. return false;
  193. }
  194. }
  195. }
  196. }