PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/kbzb/helpers/string.pm

http://kbzb.googlecode.com/
Perl | 371 lines | 127 code | 63 blank | 181 comment | 14 complexity | 213710febdbbcdcfa0b00b476726e9dc MD5 | raw file
  1. package kbzb::helpers::string;
  2. # -- $Id: string.pm 174 2009-08-06 02:11:16Z zburke $
  3. # --
  4. # -- This code originally derived from CodeIgniter 1.7.1,
  5. # -- Copyright (c) 2008, EllisLab, Inc.
  6. # --
  7. use strict;
  8. require Exporter;
  9. our @ISA = qw(Exporter);
  10. # Items to export into callers namespace by default. Note: do not export
  11. # names by default without a very good reason. Use EXPORT_OK instead.
  12. # Do not simply export all your public functions/methods/constants.
  13. # This allows declaration use asdf ':all';
  14. # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
  15. # will save memory.
  16. our %EXPORT_TAGS = ( 'all' => [ qw(
  17. trim_slashes
  18. strip_slashes
  19. strip_quotes
  20. quotes_to_entities
  21. reduce_double_slashes
  22. reduce_multiples
  23. random_string
  24. alternator
  25. repeater
  26. ) ] );
  27. our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
  28. our @EXPORT = qw(
  29. );
  30. # ------------------------------------------------------------------------
  31. # CodeIgniter String Helpers
  32. #
  33. # @package CodeIgniter
  34. # @subpackage Helpers
  35. # @category Helpers
  36. # @author ExpressionEngine Dev Team
  37. # @link http://codeigniter.com/user_guide/helpers/string_helper.html
  38. # ------------------------------------------------------------------------
  39. # Trim Slashes
  40. #
  41. # Removes any leading/traling slashes from a string:
  42. #
  43. # /this/that/theother/
  44. #
  45. # becomes:
  46. #
  47. # this/that/theother
  48. #
  49. # @access public
  50. # @param string
  51. # @return string
  52. #
  53. sub trim_slashes
  54. {
  55. my ($str) = @_;
  56. $str =~ s/^\/*|\/*$//g;
  57. return $str;
  58. }
  59. # ------------------------------------------------------------------------
  60. # Strip Slashes
  61. #
  62. # Removes slashes contained in a string or in an array
  63. #
  64. # @access public
  65. # @param mixed string or array
  66. # @return mixed string or array
  67. #
  68. sub strip_slashes
  69. {
  70. my ($str) = @_;
  71. $str ||= '';
  72. if ('HASH' eq ref $str)
  73. {
  74. for my $key (%{ $str })
  75. {
  76. $str->{$key} = strip_slashes($str->{$key});
  77. }
  78. }
  79. else
  80. {
  81. $str =~ s/\///g;
  82. }
  83. return $str;
  84. }
  85. # ------------------------------------------------------------------------
  86. # Strip Quotes
  87. #
  88. # Removes single and double quotes from a string
  89. #
  90. # @access public
  91. # @param string
  92. # @return string
  93. #
  94. sub strip_quotes
  95. {
  96. my ($str) = @_;
  97. $str =~ s/'|"//g;
  98. return $str;
  99. }
  100. # ------------------------------------------------------------------------
  101. # Quotes to Entities
  102. #
  103. # Converts single and double quotes to entities
  104. #
  105. # @access public
  106. # @param string
  107. # @return string
  108. #
  109. sub quotes_to_entities
  110. {
  111. my ($str) = @_;
  112. $str =~ s/'/'/;
  113. $str =~ s/"/"/;
  114. return $str;
  115. }
  116. # ------------------------------------------------------------------------
  117. # Reduce Double Slashes
  118. #
  119. # Converts double slashes in a string to a single slash,
  120. # except those found in http://
  121. #
  122. # http://www.some-site.com//index.php
  123. #
  124. # becomes:
  125. #
  126. # http://www.some-site.com/index.php
  127. #
  128. # @access public
  129. # @param string
  130. # @return string
  131. #
  132. sub reduce_double_slashes
  133. {
  134. my ($str) = @_;
  135. $str =~ s#([^:])//+#$1/#g;
  136. return $str;
  137. }
  138. # ------------------------------------------------------------------------
  139. # Reduce Multiples
  140. #
  141. # Reduces multiple instances of a particular character. Example:
  142. #
  143. # Fred, Bill,, Joe, Jimmy
  144. #
  145. # becomes:
  146. #
  147. # Fred, Bill, Joe, Jimmy
  148. #
  149. # @access public
  150. # @param string
  151. # @param string the character you wish to reduce
  152. # @param bool TRUE/FALSE - whether to trim the character from the beginning/end
  153. # @return string
  154. #
  155. sub reduce_multiples
  156. {
  157. my ($str, $character, $trim) = @_;
  158. $character ||= ',';
  159. $str =~ s/$character{2,}/$character/g;
  160. if ($trim)
  161. {
  162. $str =~ s/^$character*|$character*$//g;
  163. }
  164. return $str;
  165. }
  166. # ------------------------------------------------------------------------
  167. # Create a Random String
  168. #
  169. # Useful for generating passwords or hashes.
  170. #
  171. # @access public
  172. # @param string type of random string. Options: alunum, numeric, nozero, unique, hex
  173. # @param integer number of characters
  174. # @return string
  175. #
  176. sub random_string
  177. {
  178. my ($type, $len) = @_;
  179. $type ||= 'alnum';
  180. $len = 8 unless ($len && $len =~ /^[0-9]+$/ && $len > 0);
  181. if ($type eq 'unique')
  182. {
  183. #@ case 'unique' : return md5(uniqid(mt_rand()));
  184. #@ break;
  185. }
  186. else
  187. {
  188. my $pool = '';
  189. $type eq 'alnum' && ($pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  190. $type eq 'numeric' && ($pool = '0123456789');
  191. $type eq 'nozero' && ($pool = '123456789');
  192. $type eq 'hex' && ($pool = '0123456789abcdef');
  193. my $str = '';
  194. my $max = length $pool;
  195. for (0..$len)
  196. {
  197. $str .= substr($pool, int(rand($max)), 1);
  198. }
  199. return $str;
  200. }
  201. }
  202. # ------------------------------------------------------------------------
  203. # Alternator
  204. #
  205. # Allows strings to be alternated. See docs...
  206. #
  207. # @access public
  208. # @param string (as many parameters as needed)
  209. # @return string
  210. #
  211. my $alternator_i = 0;
  212. sub alternator
  213. {
  214. if (! scalar @_)
  215. {
  216. $alternator_i = 0;
  217. return '';
  218. }
  219. return $_[($alternator_i++ % scalar @_)];
  220. }
  221. # ------------------------------------------------------------------------
  222. # Repeater function
  223. #
  224. # @access public
  225. # @param string
  226. # @param integer number of repeats
  227. # @return string
  228. #
  229. sub repeater
  230. {
  231. my ($data, $num) = @_;
  232. $num = 1 unless $num && $num =~ /^[0-9]+$/ && $num > 0;
  233. return ($num > 0) ? ($data x $num) : '';
  234. }
  235. 1;
  236. __END__
  237. =pod
  238. =head1 NAME
  239. string - functions for manipulating strings
  240. =head2 USAGE
  241. # load the helper from within a controller ...
  242. $self->load->helper('string');
  243. # cycle through words on a list
  244. alternator([qw/a b c/]); # returns a
  245. alternator([qw/a b c/]); # returns b
  246. alternator([qw/a b c/]); # returns c
  247. alternator([qw/a b c/]); # returns a
  248. alternator(); # resets internal counter
  249. # convert " and ' to HTML entities
  250. $s = quotes_to_entities('I said, "You\'d better listen to me."');
  251. # generate an 8-character random alphanumeric string, i.e. [A-Za-z0-9]{8,8}
  252. $s = random_string('alnum');
  253. # generate an 8-character numeric random string
  254. $s = random_string('numeric');
  255. # generate an 8-character numeric random string without zeroes
  256. $s = random_string('nozero');
  257. # generate an 8-character hexadecimal random string, i.e. [a-f0-9]{8,8}
  258. $s = random_string('hex');
  259. # generate an $n-character random alphanumeric string
  260. $s = random_string('alnum', $n);
  261. # reduce double slashes: reduce embedded doubles, without affecting http://, https:// etc
  262. $s = reduce_double_slashes('/path/with//extra//slashes');
  263. # reduce multiple instances of a string, e.g. "foo, bar, , bat" -> "foo, bar, bat"
  264. $s = reduce_multiples('foo, bar, , bat', ', ');
  265. # reduce multiple instances of a string, and zap leading or trailing instances
  266. $s = reduce_multiples(', foo, bar, , bat', ', ');
  267. # copy a string n-times; just like perl's built-in x function
  268. $s = repeater('string', $n);
  269. # remove " and ' from a string.
  270. $s = strip_quotes('I said, "You\'d better listen to me."');
  271. # remove / from within a string
  272. $s = strip_slashes('a and/or b');
  273. # remove leading and trailing / from a string.
  274. $s = trim_slashes('/path/to/directory/');
  275. =head1 AUTHORS
  276. Zak Burke (kbzbfw@gmail.com)
  277. =head1 LICENSE
  278. This library is free software, you can redistribute it and/or modify it under
  279. the same terms as Perl itself.
  280. =cut