note 129625 added to function.str-ends-with

From: Date: Mon, 01 Jul 2024 20:25:41 +0000
Subject: note 129625 added to function.str-ends-with
Groups: php.notes 
Request: Send a blank email to php-notes+get-221402@lists.php.net to get a copy of this message
<?php Polyfill codes for str_ends_with function by MAMEDUL ISLAM from Bangladesh. // solution: 1 if( !function_exists('str_ends_with') ){ /** * 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($haystack, $needle){ if( !is_scalar($haystack) || !is_scalar($needle) ){ return false; } if( strlen($haystack) < strlen($needle) ){ return false; } return substr_compare( $haystack, $needle, -strlen($needle) ) == 0; } } // solution: 2 if( !function_exists('str_ends_with') ){ /** * 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($haystack, $needle){ if( !is_scalar($haystack) || !is_scalar($needle) ){ return false; } if( strlen($haystack) < strlen($needle) ){ return false; } return strcmp( substr( $haystack, -1*strlen($needle) ), $needle ) == 0; } } ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 37.111.222.41) ---- Manual Page -- https://php.net/manual/en/function.str-ends-with.php Edit -- https://main.php.net/note/edit/129625 Del: integrated -- https://main.php.net/note/delete/129625/integrated Del: useless -- https://main.php.net/note/delete/129625/useless Del: bad code -- https://main.php.net/note/delete/129625/bad+code Del: spam -- https://main.php.net/note/delete/129625/spam Del: non-english -- https://main.php.net/note/delete/129625/non-english Del: in docs -- https://main.php.net/note/delete/129625/in+docs Del: other reasons-- https://main.php.net/note/delete/129625 Reject -- https://main.php.net/note/reject/129625 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221402) next »