Cache
2 years ago
ThirdParty
3 years ago
DBPermissions.php
3 years ago
Escape.php
3 years ago
Math.php
5 years ago
Sanitize.php
2 years ago
ServerVars.php
3 years ago
SlashMode.php
5 years ago
Strings.php
2 years ago
Times.php
3 years ago
Urls.php
3 years ago
WpDefaultDirectories.php
2 years ago
Strings.php
129 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 | return substr_replace($subject, $replace, $pos, strlen($search)); |
| 45 | } |
| 46 | |
| 47 | return $subject; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get last string after last certain element in string |
| 52 | * Example: getLastElemAfterString('/', '/path/stagingsite/subfolder') returns 'subfolder' |
| 53 | * @param string $needle |
| 54 | * @param string $haystack |
| 55 | * @return string |
| 56 | */ |
| 57 | public function getLastElemAfterString($needle, $haystack) |
| 58 | { |
| 59 | $pos = strrpos($haystack, $needle); |
| 60 | return $pos === false ? $haystack : substr($haystack, $pos + 1); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Return url without scheme |
| 65 | * @param string $str |
| 66 | * @return string |
| 67 | */ |
| 68 | public function getUrlWithoutScheme($str) |
| 69 | { |
| 70 | return preg_replace('#^https?://#', '', rtrim($str, '/')); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Replace backward slash with forward slash directory separator |
| 75 | * Escape Windows Backward Slash - Compatibility Fix |
| 76 | * @param string $path Path |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | public function sanitizeDirectorySeparator($path) |
| 81 | { |
| 82 | $string = preg_replace('/[\\\\]+/', '/', $path); |
| 83 | return str_replace('//', '/', $string); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check if a string start with a specific string |
| 88 | * @param string $haystack |
| 89 | * @param string $needle |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function startsWith($haystack, $needle) |
| 93 | { |
| 94 | $length = strlen($needle); |
| 95 | return ($needle === substr($haystack, 0, $length)); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Check if a string ends with a specific string |
| 100 | * @param string $haystack |
| 101 | * @param string $needle |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function endsWith($haystack, $needle) |
| 105 | { |
| 106 | $haystack = strrev($haystack); |
| 107 | $needle = strrev($needle); |
| 108 | return strpos($haystack, $needle) === 0; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Search & Replace last occurrence of string in haystack |
| 113 | * @param string $needle |
| 114 | * @param string $replace |
| 115 | * @param string $haystack |
| 116 | * @return string |
| 117 | */ |
| 118 | function replaceLastMatch($needle, $replace, $haystack) |
| 119 | { |
| 120 | $result = $haystack; |
| 121 | $pos = strrpos($haystack, $needle); |
| 122 | if ($pos !== false) { |
| 123 | $result = substr_replace($haystack, $replace, $pos, strlen($needle)); |
| 124 | } |
| 125 | |
| 126 | return $result; |
| 127 | } |
| 128 | } |
| 129 |