PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.2
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Utils / Strings.php
wp-staging / Framework / Utils Last commit date
Cache 2 years ago DBPermissions.php 2 years ago DataEncoder.php 2 years ago Escape.php 2 years ago Glob.php 2 years ago Hooks.php 2 years ago Math.php 2 years ago PluginInfo.php 2 years ago Sanitize.php 2 years ago ServerVars.php 2 years ago SlashMode.php 5 years ago Strings.php 2 years ago Times.php 2 years ago Urls.php 2 years ago Version.php 2 years ago WpDefaultDirectories.php 2 years ago
Strings.php
181 lines
1 <?php
2
3 namespace WPStaging\Framework\Utils;
4
5 /**
6 * Class Strings
7 * @package WPStaging\Service\Strings
8 */
9 class Strings
10 {
11 /**
12 * Replace first occurrence of certain string
13 * @param string $search
14 * @param string $replace
15 * @param string $subject
16 * @return string
17 *
18 * @deprecated use strReplaceFirst instead
19 * @todo replace all usage of str_replace_first with strReplaceFirst
20 */
21 public function str_replace_first($search, $replace, $subject)
22 {
23 return $this->strReplaceFirst($search, $replace, $subject);
24 }
25
26 /**
27 * Replace first occurrence of certain string
28 * @param string $search
29 * @param string $replace
30 * @param string $subject
31 * @return string
32 */
33 public function strReplaceFirst($search, $replace, $subject)
34 {
35 if (empty($search)) {
36 return $subject;
37 }
38
39 $pos = strpos($subject, $search);
40 if ($pos !== false) {
41 if ($replace === null) {
42 $replace = '';
43 }
44
45 return substr_replace($subject, $replace, $pos, strlen($search));
46 }
47
48 return $subject;
49 }
50
51 /**
52 * Get last string after last certain element in string
53 * Example: getLastElemAfterString('/', '/path/stagingsite/subfolder') returns 'subfolder'
54 * @param string $needle
55 * @param string $haystack
56 * @return string
57 */
58 public function getLastElemAfterString($needle, $haystack)
59 {
60 $pos = strrpos($haystack, $needle);
61 return $pos === false ? $haystack : substr($haystack, $pos + 1);
62 }
63
64 /**
65 * Return url without scheme
66 * @param string $str
67 * @return string
68 */
69 public function getUrlWithoutScheme($str)
70 {
71 return preg_replace('#^https?://#', '', rtrim($str, '/'));
72 }
73
74 /**
75 * Replace backward slash with forward slash directory separator
76 * Escape Windows Backward Slash - Compatibility Fix
77 * @param string $path Path
78 *
79 * @return string
80 */
81 public function sanitizeDirectorySeparator($path)
82 {
83 $string = preg_replace('/[\\\\]+/', '/', $path);
84 return str_replace('//', '/', $string);
85 }
86
87 /**
88 * Check if a string start with a specific string
89 * @param string $haystack
90 * @param string $needle
91 * @return bool
92 */
93 public function startsWith($haystack, $needle)
94 {
95 $length = strlen($needle);
96 return ($needle === substr($haystack, 0, $length));
97 }
98
99 /**
100 * Check if a string start with a specific string from a list of strings
101 * @param string[] $needlesList
102 * @param string $haystack
103 * @return bool
104 */
105 public function startsWithAnyFromList(array $needlesList, string $haystack): bool
106 {
107 foreach ($needlesList as $needle) {
108 if ($this->startsWith($haystack, $needle)) {
109 return true;
110 }
111 }
112
113 return false;
114 }
115
116 /**
117 * Check if a string ends with a specific string
118 * @param string $haystack
119 * @param string $needle
120 * @return bool
121 */
122 public function endsWith($haystack, $needle)
123 {
124 $haystack = strrev($haystack);
125 $needle = strrev($needle);
126 return strpos($haystack, $needle) === 0;
127 }
128
129 /**
130 * Search & Replace last occurrence of string in haystack
131 * @param string $needle
132 * @param string $replace
133 * @param string $haystack
134 * @return string
135 */
136 public function replaceLastMatch($needle, $replace, $haystack)
137 {
138 $result = $haystack;
139 $pos = strrpos($haystack, $needle);
140 if ($pos !== false) {
141 $result = substr_replace($haystack, $replace, $pos, strlen($needle));
142 }
143
144 return $result;
145 }
146
147 /**
148 * Make sure prefix ends with underscore
149 * @param string $string
150 * @return string
151 */
152 public function maybeAppendUnderscore(string $string): string
153 {
154 // Early bail, if underscore is already the last character
155 if (substr($string, -1) === '_') {
156 return $string;
157 }
158
159 return $string . '_';
160 }
161
162 /**
163 * If $haystack starts with $needle, replace it with $replace
164 * Example: replaceStartWith('www.', '', 'www.example.com') returns 'example.com'
165 * But replaceStartWith('www.', '', 'https://wwww.example.com') returns 'https://www.example.com and remains unchanged'
166 *
167 * @param string $needle
168 * @param string $replace
169 * @param string $haystack
170 * @return string
171 */
172 public function replaceStartWith(string $needle, string $replace, string $haystack): string
173 {
174 if (strpos($haystack, $needle) === 0) {
175 return $replace . substr($haystack, strlen($needle));
176 }
177
178 return $haystack;
179 }
180 }
181