Cache
1 year ago
DBPermissions.php
1 year ago
DataEncoder.php
1 year ago
Escape.php
2 years ago
Glob.php
2 years ago
Hooks.php
1 year ago
Math.php
1 year ago
PluginInfo.php
2 years ago
Sanitize.php
2 years ago
ServerVars.php
2 years ago
SlashMode.php
5 years ago
Strings.php
1 year ago
Times.php
1 year ago
Urls.php
2 years ago
Version.php
1 year ago
WpDefaultDirectories.php
2 years ago
Strings.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Utils; |
| 4 | |
| 5 | use WPStaging\Framework\Traits\UrlTrait; |
| 6 | |
| 7 | /** |
| 8 | * Class Strings |
| 9 | * @package WPStaging\Service\Strings |
| 10 | */ |
| 11 | class Strings |
| 12 | { |
| 13 | use UrlTrait; |
| 14 | |
| 15 | /** |
| 16 | * Replace first occurrence of certain string |
| 17 | * @param string $search |
| 18 | * @param string $replace |
| 19 | * @param string $subject |
| 20 | * @return string |
| 21 | * |
| 22 | * @deprecated use strReplaceFirst instead |
| 23 | * @todo replace all usage of str_replace_first with strReplaceFirst |
| 24 | */ |
| 25 | public function str_replace_first($search, $replace, $subject) |
| 26 | { |
| 27 | return $this->strReplaceFirst($search, $replace, $subject); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Replace first occurrence of certain string |
| 32 | * @param string $search |
| 33 | * @param string $replace |
| 34 | * @param string $subject |
| 35 | * @return string |
| 36 | */ |
| 37 | public function strReplaceFirst($search, $replace, $subject) |
| 38 | { |
| 39 | if (empty($search)) { |
| 40 | return $subject; |
| 41 | } |
| 42 | |
| 43 | $pos = strpos($subject, $search); |
| 44 | if ($pos !== false) { |
| 45 | if ($replace === null) { |
| 46 | $replace = ''; |
| 47 | } |
| 48 | |
| 49 | return substr_replace($subject, $replace, $pos, strlen($search)); |
| 50 | } |
| 51 | |
| 52 | return $subject; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get last string after last certain element in string |
| 57 | * Example: getLastElemAfterString('/', '/path/stagingsite/subfolder') returns 'subfolder' |
| 58 | * @param string $needle |
| 59 | * @param string $haystack |
| 60 | * @return string |
| 61 | */ |
| 62 | public function getLastElemAfterString($needle, $haystack) |
| 63 | { |
| 64 | $pos = strrpos($haystack, $needle); |
| 65 | return $pos === false ? $haystack : substr($haystack, $pos + 1); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Replace backward slash with forward slash directory separator |
| 70 | * Escape Windows Backward Slash - Compatibility Fix |
| 71 | * @param string $path Path |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | public function sanitizeDirectorySeparator($path) |
| 76 | { |
| 77 | $string = preg_replace('/[\\\\]+/', '/', $path); |
| 78 | return str_replace('//', '/', $string); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Check if a string start with a specific string |
| 83 | * @param string $haystack |
| 84 | * @param string $needle |
| 85 | * @return bool |
| 86 | */ |
| 87 | public function startsWith($haystack, $needle) |
| 88 | { |
| 89 | $length = strlen($needle); |
| 90 | return ($needle === substr($haystack, 0, $length)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Check if a string start with a specific string from a list of strings |
| 95 | * @param string[] $needlesList |
| 96 | * @param string $haystack |
| 97 | * @return bool |
| 98 | */ |
| 99 | public function startsWithAnyFromList(array $needlesList, string $haystack): bool |
| 100 | { |
| 101 | foreach ($needlesList as $needle) { |
| 102 | if ($this->startsWith($haystack, $needle)) { |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Check if a string ends with a specific string |
| 112 | * @param string $haystack |
| 113 | * @param string $needle |
| 114 | * @return bool |
| 115 | */ |
| 116 | public function endsWith($haystack, $needle) |
| 117 | { |
| 118 | $haystack = strrev($haystack); |
| 119 | $needle = strrev($needle); |
| 120 | return strpos($haystack, $needle) === 0; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Search & Replace last occurrence of string in haystack |
| 125 | * @param string $needle |
| 126 | * @param string $replace |
| 127 | * @param string $haystack |
| 128 | * @return string |
| 129 | */ |
| 130 | public function replaceLastMatch($needle, $replace, $haystack) |
| 131 | { |
| 132 | $result = $haystack; |
| 133 | $pos = strrpos($haystack, $needle); |
| 134 | if ($pos !== false) { |
| 135 | $result = substr_replace($haystack, $replace, $pos, strlen($needle)); |
| 136 | } |
| 137 | |
| 138 | return $result; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Make sure prefix ends with underscore |
| 143 | * @param string $string |
| 144 | * @return string |
| 145 | */ |
| 146 | public function maybeAppendUnderscore(string $string): string |
| 147 | { |
| 148 | // Early bail, if underscore is already the last character |
| 149 | if (substr($string, -1) === '_') { |
| 150 | return $string; |
| 151 | } |
| 152 | |
| 153 | return $string . '_'; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * If $haystack starts with $needle, replace it with $replace |
| 158 | * Example: replaceStartWith('www.', '', 'www.example.com') returns 'example.com' |
| 159 | * But replaceStartWith('www.', '', 'https://wwww.example.com') returns 'https://www.example.com and remains unchanged' |
| 160 | * |
| 161 | * @param string $needle |
| 162 | * @param string $replace |
| 163 | * @param string $haystack |
| 164 | * @return string |
| 165 | */ |
| 166 | public function replaceStartWith(string $needle, string $replace, string $haystack): string |
| 167 | { |
| 168 | if (strpos($haystack, $needle) === 0) { |
| 169 | return $replace . substr($haystack, strlen($needle)); |
| 170 | } |
| 171 | |
| 172 | return $haystack; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param string $email |
| 177 | * @return string |
| 178 | */ |
| 179 | public function maskEmail(string $email): string |
| 180 | { |
| 181 | if (empty($email)) { |
| 182 | return ''; |
| 183 | } |
| 184 | |
| 185 | list($username, $domain) = explode('@', $email); |
| 186 | $firstChar = substr($username, 0, 1); |
| 187 | $lastChar = substr($username, -1); |
| 188 | $maskedUsername = $firstChar . str_repeat('*', strlen($username) - 2) . $lastChar; |
| 189 | list($domainName, $tld) = explode('.', $domain); |
| 190 | $maskedDomainName = substr($domainName, 0, 1) . str_repeat('*', strlen($domainName) - 1); |
| 191 | return $maskedUsername . '@' . $maskedDomainName . '.' . $tld; |
| 192 | } |
| 193 | |
| 194 | } |
| 195 |