|
@@ 5131-5189 (lines=59) @@
|
| 5128 |
|
* |
| 5129 |
|
* @return false|string A sub-string,<br />or <strong>false</strong> if needle is not found. |
| 5130 |
|
*/ |
| 5131 |
|
public static function stristr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false) |
| 5132 |
|
{ |
| 5133 |
|
$haystack = (string)$haystack; |
| 5134 |
|
$needle = (string)$needle; |
| 5135 |
|
$before_needle = (bool)$before_needle; |
| 5136 |
|
|
| 5137 |
|
if (!isset($haystack[0], $needle[0])) { |
| 5138 |
|
return false; |
| 5139 |
|
} |
| 5140 |
|
|
| 5141 |
|
if ($encoding !== 'UTF-8') { |
| 5142 |
|
$encoding = self::normalize_encoding($encoding, 'UTF-8'); |
| 5143 |
|
} |
| 5144 |
|
|
| 5145 |
|
if ($cleanUtf8 === true) { |
| 5146 |
|
// "\mb_strpos" and "\iconv_strpos" returns wrong position, |
| 5147 |
|
// if invalid characters are found in $haystack before $needle |
| 5148 |
|
$needle = self::clean($needle); |
| 5149 |
|
$haystack = self::clean($haystack); |
| 5150 |
|
} |
| 5151 |
|
|
| 5152 |
|
if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) { |
| 5153 |
|
self::checkForSupport(); |
| 5154 |
|
} |
| 5155 |
|
|
| 5156 |
|
if ( |
| 5157 |
|
$encoding !== 'UTF-8' |
| 5158 |
|
&& |
| 5159 |
|
self::$SUPPORT['mbstring'] === false |
| 5160 |
|
) { |
| 5161 |
|
trigger_error('UTF8::stristr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); |
| 5162 |
|
} |
| 5163 |
|
|
| 5164 |
|
if (self::$SUPPORT['mbstring'] === true) { |
| 5165 |
|
return \mb_stristr($haystack, $needle, $before_needle, $encoding); |
| 5166 |
|
} |
| 5167 |
|
|
| 5168 |
|
if ( |
| 5169 |
|
$encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings |
| 5170 |
|
&& |
| 5171 |
|
self::$SUPPORT['intl'] === true |
| 5172 |
|
&& |
| 5173 |
|
Bootup::is_php('5.4') === true |
| 5174 |
|
) { |
| 5175 |
|
return \grapheme_stristr($haystack, $needle, $before_needle); |
| 5176 |
|
} |
| 5177 |
|
|
| 5178 |
|
preg_match('/^(.*?)' . preg_quote($needle, '/') . '/usi', $haystack, $match); |
| 5179 |
|
|
| 5180 |
|
if (!isset($match[1])) { |
| 5181 |
|
return false; |
| 5182 |
|
} |
| 5183 |
|
|
| 5184 |
|
if ($before_needle) { |
| 5185 |
|
return $match[1]; |
| 5186 |
|
} |
| 5187 |
|
|
| 5188 |
|
return self::substr($haystack, self::strlen($match[1])); |
| 5189 |
|
} |
| 5190 |
|
|
| 5191 |
|
/** |
| 5192 |
|
* Get the string length, not the byte-length! |
|
@@ 5844-5901 (lines=58) @@
|
| 5841 |
|
* |
| 5842 |
|
* @return string|false A sub-string,<br />or <strong>false</strong> if needle is not found. |
| 5843 |
|
*/ |
| 5844 |
|
public static function strstr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false) |
| 5845 |
|
{ |
| 5846 |
|
$haystack = (string)$haystack; |
| 5847 |
|
$needle = (string)$needle; |
| 5848 |
|
|
| 5849 |
|
if (!isset($haystack[0], $needle[0])) { |
| 5850 |
|
return false; |
| 5851 |
|
} |
| 5852 |
|
|
| 5853 |
|
if ($cleanUtf8 === true) { |
| 5854 |
|
// "\mb_strpos" and "\iconv_strpos" returns wrong position, |
| 5855 |
|
// if invalid characters are found in $haystack before $needle |
| 5856 |
|
$needle = self::clean($needle); |
| 5857 |
|
$haystack = self::clean($haystack); |
| 5858 |
|
} |
| 5859 |
|
|
| 5860 |
|
if ($encoding !== 'UTF-8') { |
| 5861 |
|
$encoding = self::normalize_encoding($encoding, 'UTF-8'); |
| 5862 |
|
} |
| 5863 |
|
|
| 5864 |
|
if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) { |
| 5865 |
|
self::checkForSupport(); |
| 5866 |
|
} |
| 5867 |
|
|
| 5868 |
|
if ( |
| 5869 |
|
$encoding !== 'UTF-8' |
| 5870 |
|
&& |
| 5871 |
|
self::$SUPPORT['mbstring'] === false |
| 5872 |
|
) { |
| 5873 |
|
trigger_error('UTF8::strstr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); |
| 5874 |
|
} |
| 5875 |
|
|
| 5876 |
|
if (self::$SUPPORT['mbstring'] === true) { |
| 5877 |
|
return \mb_strstr($haystack, $needle, $before_needle, $encoding); |
| 5878 |
|
} |
| 5879 |
|
|
| 5880 |
|
if ( |
| 5881 |
|
$encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings |
| 5882 |
|
&& |
| 5883 |
|
self::$SUPPORT['intl'] === true |
| 5884 |
|
&& |
| 5885 |
|
Bootup::is_php('5.4') === true |
| 5886 |
|
) { |
| 5887 |
|
return \grapheme_strstr($haystack, $needle, $before_needle); |
| 5888 |
|
} |
| 5889 |
|
|
| 5890 |
|
preg_match('/^(.*?)' . preg_quote($needle, '/') . '/us', $haystack, $match); |
| 5891 |
|
|
| 5892 |
|
if (!isset($match[1])) { |
| 5893 |
|
return false; |
| 5894 |
|
} |
| 5895 |
|
|
| 5896 |
|
if ($before_needle) { |
| 5897 |
|
return $match[1]; |
| 5898 |
|
} |
| 5899 |
|
|
| 5900 |
|
return self::substr($haystack, self::strlen($match[1])); |
| 5901 |
|
} |
| 5902 |
|
|
| 5903 |
|
/** |
| 5904 |
|
* Unicode transformation for case-less matching. |