Last active
January 3, 2022 04:50
-
-
Save ve3/6159b02bba8f37d06fbcfa0e98aae778 to your computer and use it in GitHub Desktop.
Revisions
-
ve3 revised this gist
Jan 3, 2022 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ /** * Determine if a string contains a given substring * * @link https://www.php.net/manual/en/function.str-contains.php PHP `str_contains` original document. * @param string $haystack The string to search in. * @param string $needle The substring to search for in the `haystack`. * @return bool Returns `true` if `needle` is in `haystack`, `false` otherwise. -
ve3 revised this gist
Jan 3, 2022 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,13 @@ <?php if (!function_exists('str_contains')) { /** * Determine if a string contains a given substring * * @param string $haystack The string to search in. * @param string $needle The substring to search for in the `haystack`. * @return bool Returns `true` if `needle` is in `haystack`, `false` otherwise. */ function str_contains($haystack, $needle) { // verify type and show the errors as in PHP 8. if (!is_scalar($haystack)) { @@ -11,5 +18,8 @@ function str_contains($haystack, $needle) { } return '' === $needle || false === $needle || false !== mb_strpos($haystack, $needle); // note: // empty string and `false` always return `true` in PHP 8.0+. // `true`, float (or double) type must be check with `mb_strpos()` instead of `strpos()`. } } -
ve3 created this gist
Jan 3, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ <?php if (!function_exists('str_contains')) { function str_contains($haystack, $needle) { // verify type and show the errors as in PHP 8. if (!is_scalar($haystack)) { trigger_error('Argument #1 ($haystack) must be of type string, ' . gettype($haystack) . ' given', E_USER_ERROR); } if (!is_scalar($needle)) { trigger_error('Argument #2 ($needle) must be of type string, ' . gettype($needle) . ' given', E_USER_ERROR); } return '' === $needle || false === $needle || false !== mb_strpos($haystack, $needle); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ <?php $string = '123abcกขคょよら㑆㑇㑈456.78'; echo 'haystack is ' . $string . '<br>'; echo 'false, true, empty<br>'; var_dump(str_contains($string, false));// true var_dump(str_contains($string, true));// false var_dump(str_contains($string, ''));// true echo 'contains strings<br>'; var_dump(str_contains($string, 'a'));// true var_dump(str_contains($string, 'aa'));// false var_dump(str_contains($string, 'z'));// false var_dump(str_contains($string, 'ก'));// true var_dump(str_contains($string, 'กก'));// false var_dump(str_contains($string, 'ฮ'));// false var_dump(str_contains($string, 'ょ'));// true var_dump(str_contains($string, 'ょょ'));// false var_dump(str_contains($string, 'ゖ'));// false var_dump(str_contains($string, '㑆'));// true var_dump(str_contains($string, '㑆㑆'));// false var_dump(str_contains($string, '㔩'));// false echo 'contains 0 & 1<br>'; var_dump(str_contains($string, 0));// false var_dump(str_contains($string, 1));// true echo 'contains numbers (float, double)<br>'; var_dump(str_contains($string, 123.0));// true (required mb_strpos) var_dump(str_contains($string, 456.78));// true (required mb_strpos) echo 'haystack is true. check contains false, true, empty<br>'; var_dump(str_contains(true, false));// true var_dump(str_contains(true, true));// true (required mb_strpos) var_dump(str_contains(true, ''));// true echo 'haystack is false. check contains false, true, empty<br>'; var_dump(str_contains(false, false));// true var_dump(str_contains(false, true));// false var_dump(str_contains(false, ''));// true echo 'haystack is empty. check contains false, true, empty<br>'; var_dump(str_contains('', false));// true var_dump(str_contains('', true));// false var_dump(str_contains('', ''));// true echo 'error!<br>'; var_dump(str_contains($string, ['a'])); echo 'can\'t continue here.<br>';