|
@@ 5224-5282 (lines=59) @@
|
| 5221 |
|
* |
| 5222 |
|
* @return false|string A sub-string,<br />or <strong>false</strong> if needle is not found. |
| 5223 |
|
*/ |
| 5224 |
|
public static function stristr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false) |
| 5225 |
|
{ |
| 5226 |
|
$haystack = (string)$haystack; |
| 5227 |
|
$needle = (string)$needle; |
| 5228 |
|
$before_needle = (bool)$before_needle; |
| 5229 |
|
|
| 5230 |
|
if (!isset($haystack[0], $needle[0])) { |
| 5231 |
|
return false; |
| 5232 |
|
} |
| 5233 |
|
|
| 5234 |
|
if ($encoding !== 'UTF-8') { |
| 5235 |
|
$encoding = self::normalize_encoding($encoding, 'UTF-8'); |
| 5236 |
|
} |
| 5237 |
|
|
| 5238 |
|
if ($cleanUtf8 === true) { |
| 5239 |
|
// "\mb_strpos" and "\iconv_strpos" returns wrong position, |
| 5240 |
|
// if invalid characters are found in $haystack before $needle |
| 5241 |
|
$needle = self::clean($needle); |
| 5242 |
|
$haystack = self::clean($haystack); |
| 5243 |
|
} |
| 5244 |
|
|
| 5245 |
|
if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) { |
| 5246 |
|
self::checkForSupport(); |
| 5247 |
|
} |
| 5248 |
|
|
| 5249 |
|
if ( |
| 5250 |
|
$encoding !== 'UTF-8' |
| 5251 |
|
&& |
| 5252 |
|
self::$SUPPORT['mbstring'] === false |
| 5253 |
|
) { |
| 5254 |
|
trigger_error('UTF8::stristr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); |
| 5255 |
|
} |
| 5256 |
|
|
| 5257 |
|
if (self::$SUPPORT['mbstring'] === true) { |
| 5258 |
|
return \mb_stristr($haystack, $needle, $before_needle, $encoding); |
| 5259 |
|
} |
| 5260 |
|
|
| 5261 |
|
if ( |
| 5262 |
|
$encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings |
| 5263 |
|
&& |
| 5264 |
|
self::$SUPPORT['intl'] === true |
| 5265 |
|
&& |
| 5266 |
|
Bootup::is_php('5.4') === true |
| 5267 |
|
) { |
| 5268 |
|
return \grapheme_stristr($haystack, $needle, $before_needle); |
| 5269 |
|
} |
| 5270 |
|
|
| 5271 |
|
preg_match('/^(.*?)' . preg_quote($needle, '/') . '/usi', $haystack, $match); |
| 5272 |
|
|
| 5273 |
|
if (!isset($match[1])) { |
| 5274 |
|
return false; |
| 5275 |
|
} |
| 5276 |
|
|
| 5277 |
|
if ($before_needle) { |
| 5278 |
|
return $match[1]; |
| 5279 |
|
} |
| 5280 |
|
|
| 5281 |
|
return self::substr($haystack, self::strlen($match[1])); |
| 5282 |
|
} |
| 5283 |
|
|
| 5284 |
|
/** |
| 5285 |
|
* Get the string length, not the byte-length! |
|
@@ 5949-6006 (lines=58) @@
|
| 5946 |
|
* |
| 5947 |
|
* @return string|false A sub-string,<br />or <strong>false</strong> if needle is not found. |
| 5948 |
|
*/ |
| 5949 |
|
public static function strstr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false) |
| 5950 |
|
{ |
| 5951 |
|
$haystack = (string)$haystack; |
| 5952 |
|
$needle = (string)$needle; |
| 5953 |
|
|
| 5954 |
|
if (!isset($haystack[0], $needle[0])) { |
| 5955 |
|
return false; |
| 5956 |
|
} |
| 5957 |
|
|
| 5958 |
|
if ($cleanUtf8 === true) { |
| 5959 |
|
// "\mb_strpos" and "\iconv_strpos" returns wrong position, |
| 5960 |
|
// if invalid characters are found in $haystack before $needle |
| 5961 |
|
$needle = self::clean($needle); |
| 5962 |
|
$haystack = self::clean($haystack); |
| 5963 |
|
} |
| 5964 |
|
|
| 5965 |
|
if ($encoding !== 'UTF-8') { |
| 5966 |
|
$encoding = self::normalize_encoding($encoding, 'UTF-8'); |
| 5967 |
|
} |
| 5968 |
|
|
| 5969 |
|
if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) { |
| 5970 |
|
self::checkForSupport(); |
| 5971 |
|
} |
| 5972 |
|
|
| 5973 |
|
if ( |
| 5974 |
|
$encoding !== 'UTF-8' |
| 5975 |
|
&& |
| 5976 |
|
self::$SUPPORT['mbstring'] === false |
| 5977 |
|
) { |
| 5978 |
|
trigger_error('UTF8::strstr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING); |
| 5979 |
|
} |
| 5980 |
|
|
| 5981 |
|
if (self::$SUPPORT['mbstring'] === true) { |
| 5982 |
|
return \mb_strstr($haystack, $needle, $before_needle, $encoding); |
| 5983 |
|
} |
| 5984 |
|
|
| 5985 |
|
if ( |
| 5986 |
|
$encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings |
| 5987 |
|
&& |
| 5988 |
|
self::$SUPPORT['intl'] === true |
| 5989 |
|
&& |
| 5990 |
|
Bootup::is_php('5.4') === true |
| 5991 |
|
) { |
| 5992 |
|
return \grapheme_strstr($haystack, $needle, $before_needle); |
| 5993 |
|
} |
| 5994 |
|
|
| 5995 |
|
preg_match('/^(.*?)' . preg_quote($needle, '/') . '/us', $haystack, $match); |
| 5996 |
|
|
| 5997 |
|
if (!isset($match[1])) { |
| 5998 |
|
return false; |
| 5999 |
|
} |
| 6000 |
|
|
| 6001 |
|
if ($before_needle) { |
| 6002 |
|
return $match[1]; |
| 6003 |
|
} |
| 6004 |
|
|
| 6005 |
|
return self::substr($haystack, self::strlen($match[1])); |
| 6006 |
|
} |
| 6007 |
|
|
| 6008 |
|
/** |
| 6009 |
|
* Unicode transformation for case-less matching. |