note 129624 added to function.str-starts-with

From: Date: Mon, 01 Jul 2024 20:23:24 +0000
Subject: note 129624 added to function.str-starts-with
Groups: php.notes 
Request: Send a blank email to php-notes+get-221401@lists.php.net to get a copy of this message
<?php //Polyfill codes for str_starts_with function by MAMEDUL ISLAM from Bangladesh. // previous answer is wrong // solution: 1 if( !function_exists('str_starts_with') ){ /** * str_starts_with — Checks if a string starts with 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 haystack begins with needle, false otherwise. */ function str_starts_with($haystack, $needle){ if( !is_scalar($haystack) || !is_scalar($needle) ){ return false; } if( strlen($haystack) < strval($needle) ){ return false; } return substr_compare( $haystack, $needle, -strlen($needle) ) == 0; } } // solution: 2 if( !function_exists('str_starts_with') ){ /** * str_starts_with — Checks if a string starts with 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 haystack begins with needle, false otherwise. */ function str_starts_with($haystack, $needle){ if( !is_scalar($haystack) || !is_scalar($needle) ){ return false; } if( strlen($haystack) < strlen($needle) ){ return false; } return strcmp( substr( $haystack, 0, 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-starts-with.php Edit -- https://main.php.net/note/edit/129624 Del: integrated -- https://main.php.net/note/delete/129624/integrated Del: useless -- https://main.php.net/note/delete/129624/useless Del: bad code -- https://main.php.net/note/delete/129624/bad+code Del: spam -- https://main.php.net/note/delete/129624/spam Del: non-english -- https://main.php.net/note/delete/129624/non-english Del: in docs -- https://main.php.net/note/delete/129624/in+docs Del: other reasons-- https://main.php.net/note/delete/129624 Reject -- https://main.php.net/note/reject/129624 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221401) next »