1: <?php
2: /**
3: * This file contains the string utility class.
4: *
5: * @package Core
6: * @subpackage Util
7: * @version SVN Revision $Rev:$
8: *
9: * @author Murat Purc <murat@purc.de>
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * String helper class.
20: *
21: * @package Core
22: * @subpackage Util
23: */
24: class cString {
25:
26: /**
27: * Replaces a string only once
28: *
29: * Caution: This function only takes strings as parameters, not arrays!
30: *
31: * @param string $find String to find
32: * @param string $replace String to replace
33: * @param string $subject String to process
34: * @return string Processed string
35: */
36: public static function iReplaceOnce($find, $replace, $subject) {
37: $start = strpos(strtolower($subject), strtolower($find));
38:
39: if ($start === false) {
40: return $subject;
41: }
42:
43: $end = $start + strlen($find);
44: $first = substr($subject, 0, $start);
45: $last = substr($subject, $end, strlen($subject) - $end);
46:
47: $result = $first . $replace . $last;
48:
49: return $result;
50: }
51:
52: /**
53: * Replaces a string only once, in reverse direction
54: *
55: * Caution: This function only takes strings as parameters, not arrays!
56: *
57: * @param string $find String to find
58: * @param string $replace String to replace
59: * @param string $subject String to process
60: * @return string Processed string
61: */
62: public static function iReplaceOnceReverse($find, $replace, $subject) {
63: $start = self::posReverse(strtolower($subject), strtolower($find));
64:
65: if ($start === false) {
66: return $subject;
67: }
68:
69: $end = $start + strlen($find);
70:
71: $first = substr($subject, 0, $start);
72: $last = substr($subject, $end, strlen($subject) - $end);
73:
74: $result = $first . $replace . $last;
75:
76: return ($result);
77: }
78:
79: /**
80: * Finds a string position in reverse direction
81: *
82: * NOTE: The original strrpos-Function of PHP4 only finds a single character as needle.
83: *
84: * @param string $haystack String to search in
85: * @param string $needle String to search for
86: * @param int $start Offset
87: * @return string Processed string
88: */
89: public static function posReverse($haystack, $needle, $start = 0) {
90: $tempPos = strpos($haystack, $needle, $start);
91:
92: if ($tempPos === false) {
93: if ($start == 0) {
94: // Needle not in string at all
95: return false;
96: } else {
97: // No more occurances found
98: return $start - strlen($needle);
99: }
100: } else {
101: // Find the next occurance
102: return self::posReverse($haystack, $needle, $tempPos + strlen($needle));
103: }
104: }
105:
106: /**
107: * Adds slashes to passed variable or array.
108: *
109: * @param string|array $value Either a string or a multi-dimensional array of values
110: * @return string|array
111: */
112: public static function addSlashes($value) {
113: $value = is_array($value) ? array_map(array('cString', 'addSlashes'), $value) : addslashes($value);
114: return $value;
115: }
116:
117: /**
118: * Removes slashes from passed variable or array.
119: *
120: * @param string|array $value Either a string or a multi-dimensional array of values
121: * @return string|array
122: */
123: public static function stripSlashes($value) {
124: $value = is_array($value) ? array_map(array('cString', 'stripSlashes'), $value) : stripslashes($value);
125: return $value;
126: }
127:
128: /**
129: * Checks if the string haystack ends with needle
130: *
131: * @param string $haystack The string to check
132: * @param string $needle The string with which it should end
133: * @return bool
134: */
135: public static function endsWith($haystack, $needle) {
136: $length = strlen($needle);
137: if ($length == 0) {
138: return true;
139: }
140:
141: return (substr($haystack, -$length) === $needle);
142: }
143:
144: /**
145: * Returns true if needle can be found in haystack
146: *
147: * @param string $haystack String to be searched
148: * @param string $needle String to search for
149: * @return boolean
150: */
151: public static function contains($haystack, $needle) {
152: return !(strpos($haystack, $needle) === false);
153: }
154:
155: /**
156: * Implementation of PHP 5.3's strstr with beforeNeedle
157: *
158: * @param string $haystack String to be searched
159: * @param string $needle String to search for
160: * @param string $beforeNeedle If true, return everything BEFORE needle
161: * @return string
162: * @link http://php.net/manual/de/function.strstr.php
163: */
164: public static function strstr($haystack, $needle, $beforeNeedle = false) {
165: if (!$beforeNeedle) {
166: return strstr($haystack, $needle);
167: } else {
168: return strtok($haystack, $needle);
169: }
170: }
171: }
172: