Documentation is available at RSStringUtil.php
1 <?php
2 // RSStringUtil: A String Utility Class
3 // Copyright (C) 2003 Lukas Feiler
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 /**This file contains the class RSStringUtil.
20 *
21 * @package RSStringUtil
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**Some methods of the static class RSStringUtil require this class.
28 */
29 require_once('RSValidation/RSValidation.php');
30
31 /**A Utility Class for Manipulating Strings.
32 *
33 * General order of arguments for all methods:<br>
34 * The string to operate on is always the first argument.
35 *
36 * @author Lukas Feiler <lukas.feiler@chello.at>
37 * @version 0.1.9
38 * @copyright Lukas Feiler 2003
39 * @package RSStringUtil
40 * @static
41 */
42 class RSStringUtil {
43
44 /**Returns the string passed as argument with the trailing slash removed.
45 *
46 * If the passed string does not end with a slash the passed string is returned without modification.
47 * NOTE: This method only deals with foreward slashes (/).
48 *
49 * @param String $str
50 * @returns String
51 */
52 function removeTrailingString($str, $trailing)
53 {
54 if ($trailing != "" && RSValidation::endsWith($str, $trailing)) {
55 return substr($str, 0, strlen($str) - strlen($trailing));
56 }
57 return $str;
58 }
59
60 /**Returns the string passed as argument with the leading dollar sign ($) removed removed.
61 *
62 * @param String $str
63 * @return String
64 */
65 function removeLeadingString($str,$leading)
66 {
67 if ($leading != "" && RSValidation::startsWith($str, $leading)) {
68 return substr($str,strlen($leading));
69 }
70 return $str;
71 }
72
73 /**Returns the first argument divided by 1024 with a precision specified by the second argument. If the second argument is not present the default of 1 is assumed.
74 *
75 * @param mixed $byte Number of bytes.
76 * @return int The first argument divided by 1024.
77 */
78 function bytesToKiloBytes($byte, $precision = 1)
79 {
80 $kiloByte = $byte/1024;
81 return round($kiloByte,$precision);
82 }
83
84 /**Returns the first argument limited to as many characters as specified by the second argument.
85 *
86 * The third argument is appended to the limited string. Its default is '...'.
87 *
88 * @param String $str The string to limit.
89 * @return String The limited string with the third argument appended.
90 */
91 function limit($str, $limit, $appendString = '...')
92 {
93 if (strlen($str) > $limit) {
94 $str = substr($str, 0, $limit) . $appendString;
95 }
96 return $str;
97 }
98
99 /**Returns the index of the last occurrence of the string passed as second argument in the string passed as first argument.
100 *
101 * @param String $str
102 * @param String $search
103 */
104 function lastIndexOf($str, $search)
105 {
106 if (strpos($str, $search) === false) {
107 return false;
108 }
109 return strlen($str) - strlen(strrchr($str, $search));
110 }
111 }
112 ?>
Documentation generated on Mon, 8 Dec 2003 13:13:09 +0100 by phpDocumentor 1.2.3