PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.2.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.2.0
4.9.3 4.9.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 ThirdParty 2 years ago DBPermissions.php 2 years ago Escape.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 3 years ago WpDefaultDirectories.php 2 years ago
Strings.php
130 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 ends with a specific string
101 * @param string $haystack
102 * @param string $needle
103 * @return bool
104 */
105 public function endsWith($haystack, $needle)
106 {
107 $haystack = strrev($haystack);
108 $needle = strrev($needle);
109 return strpos($haystack, $needle) === 0;
110 }
111
112 /**
113 * Search & Replace last occurrence of string in haystack
114 * @param string $needle
115 * @param string $replace
116 * @param string $haystack
117 * @return string
118 */
119 function replaceLastMatch($needle, $replace, $haystack)
120 {
121 $result = $haystack;
122 $pos = strrpos($haystack, $needle);
123 if ($pos !== false) {
124 $result = substr_replace($haystack, $replace, $pos, strlen($needle));
125 }
126
127 return $result;
128 }
129 }
130