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: