| 1 |
<?php |
| 2 |
/** |
| 3 |
* Polyfills for PHP 8.0 string functions. |
| 4 |
* |
| 5 |
* Implementation follows the Symfony polyfill-php80 package. |
| 6 |
* |
| 7 |
* @see https://github.com/symfony/polyfill-php80 |
| 8 |
* |
| 9 |
* @package wp-sqlite-integration |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! function_exists( 'str_starts_with' ) ) { |
| 13 |
/** |
| 14 |
* Check if a string starts with a specific substring. |
| 15 |
* |
| 16 |
* @param string $haystack The string to search in. |
| 17 |
* @param string $needle The string to search for. |
| 18 |
* |
| 19 |
* @see https://www.php.net/manual/en/function.str-starts-with |
| 20 |
* |
| 21 |
* @return bool |
| 22 |
*/ |
| 23 |
function str_starts_with( string $haystack, string $needle ) { |
| 24 |
return 0 === strncmp( $haystack, $needle, strlen( $needle ) ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
if ( ! function_exists( 'str_contains' ) ) { |
| 29 |
/** |
| 30 |
* Check if a string contains a specific substring. |
| 31 |
* |
| 32 |
* @param string $haystack The string to search in. |
| 33 |
* @param string $needle The string to search for. |
| 34 |
* |
| 35 |
* @see https://www.php.net/manual/en/function.str-contains |
| 36 |
* |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
function str_contains( string $haystack, string $needle ) { |
| 40 |
return '' === $needle || false !== strpos( $haystack, $needle ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! function_exists( 'str_ends_with' ) ) { |
| 45 |
/** |
| 46 |
* Check if a string ends with a specific substring. |
| 47 |
* |
| 48 |
* @param string $haystack The string to search in. |
| 49 |
* @param string $needle The string to search for. |
| 50 |
* |
| 51 |
* @see https://www.php.net/manual/en/function.str-ends-with |
| 52 |
* |
| 53 |
* @return bool |
| 54 |
*/ |
| 55 |
function str_ends_with( string $haystack, string $needle ) { |
| 56 |
if ( '' === $needle || $needle === $haystack ) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
if ( '' === $haystack ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
$needle_length = strlen( $needle ); |
| 65 |
|
| 66 |
return $needle_length <= strlen( $haystack ) && 0 === substr_compare( $haystack, $needle, -$needle_length ); |
| 67 |
} |
| 68 |
} |
| 69 |
|