From a9886a8ba95a3bac3af95616632b75319d788e78 Mon Sep 17 00:00:00 2001 From: frytimo Date: Tue, 26 Mar 2024 19:42:44 -0300 Subject: [PATCH] Add str_starts_with and str_ends_with to PHP versions older than 8 (#6832) * Add str_starts_with and str_ends_with to PHP versions older than 8 * Update functions.php --- resources/functions.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/resources/functions.php b/resources/functions.php index d157dfd9b2..9775a57ac9 100644 --- a/resources/functions.php +++ b/resources/functions.php @@ -23,8 +23,36 @@ Contributor(s): Mark J Crane + Tim Fry Luis Daniel Lucio Quiroz - */ + */ + + if (!function_exists('str_starts_with')) { + /** + * Checks if a string starts with a given substring + *

Performs a case-sensitive check indicating if haystack begins with needle.

+ * @param string $haystack The string to search in. + * @param string $needle The substring to search for in the haystack. + * @return bool Returns true if haystack begins with needle, false otherwise + * @link https://www.php.net/manual/en/function.str-starts-with.php Official PHP documentation + */ + function str_starts_with(string $haystack, string $needle): bool { + return substr_compare($haystack, $needle, 0, strlen($needle)) === 0; + } + } + + if (!function_exists('str_ends_with')) { + /** + * Checks if a string ends with a given substring + *

Performs a case-sensitive check indicating if haystack ends with needle.

+ * @param string $haystack The string to search in. + * @param string $needle The substring to search for in the haystack. + * @return bool Returns true if haystack ends with needle, false otherwise. + */ + function str_ends_with(string $haystack, string $needle): bool { + return substr_compare($haystack, $needle, -1*strlen($needle)) === 0; + } + } if (!function_exists('mb_strtoupper')) {