LoliPHP/helper/php8compat.php

35 行
1.1 KiB
PHP
Raw パーマリンク 通常表示 履歴

2022-03-27 02:41:32 +09:00
<?php
2022-10-25 18:09:58 +09:00
if (!function_exists("str_starts_with")) {
2022-03-27 02:41:32 +09:00
function str_starts_with (string $haystack, string $needle): bool {
2022-10-25 18:09:58 +09:00
return (string)$needle !== "" && strncmp($haystack, $needle, strlen($needle)) === 0;
2022-03-27 02:41:32 +09:00
}
}
2022-10-25 18:09:58 +09:00
if (!function_exists("str_ends_with")) {
2022-03-27 02:41:32 +09:00
function str_ends_with (string $haystack, string $needle): bool {
$needle_len = strlen($needle);
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len));
}
}
2022-10-25 18:09:58 +09:00
if (!function_exists("str_contains")) {
2022-03-27 02:41:32 +09:00
function str_contains (string $haystack, string $needle): bool {
2022-10-25 18:09:58 +09:00
return $needle !== "" && mb_strpos($haystack, $needle) !== false;
2022-03-27 02:41:32 +09:00
}
}
2022-10-25 18:09:58 +09:00
if (!function_exists("array_key_first")) {
2022-03-27 02:41:32 +09:00
function array_key_first (array $array): int|string|null {
if (!is_array($array) || empty($array)) return NULL;
return array_keys($array)[0];
}
}
2022-10-25 18:09:58 +09:00
if (!function_exists("array_key_last")) {
2022-03-27 02:41:32 +09:00
function array_key_last (array $array): int|string|null {
if (!is_array($array) || empty($array)) return NULL;
return array_keys($array)[count($array)-1];
}
}
?>