note 128961 added to function.str-starts-with

From: Date: Sun, 15 Oct 2023 13:30:51 +0000
Subject: note 128961 added to function.str-starts-with
Groups: php.notes 
Request: Send a blank email to php-notes+get-220920@lists.php.net to get a copy of this message
I had the task of creating the most approximate polyfill of "str_starts_with()" for the open CMS EffCore and the result was the following code: namespace { if (!function_exists('str_starts_with')) { function str_starts_with($haystack, $needle) { if (is_null ($haystack)) trigger_error('str_starts_with(): Passing null to parameter #1 ($haystack) of type string is deprecated in '.__FILE__, E_USER_DEPRECATED); if (is_null ($needle) ) trigger_error('str_starts_with(): Passing null to parameter #2 ($needle) of type string is deprecated in '.__FILE__, E_USER_DEPRECATED); if (is_array ($haystack)) throw new TypeError('str_starts_with(): Argument #1 ($haystack) must be of type string, array given'); if (is_object ($haystack)) throw new TypeError('str_starts_with(): Argument #1 ($haystack) must be of type string, object given'); if (is_resource($haystack)) throw new TypeError('str_starts_with(): Argument #1 ($haystack) must be of type string, resource given'); if (is_array ($needle) ) throw new TypeError('str_starts_with(): Argument #2 ($needle) must be of type string, array given'); if (is_object ($needle) ) throw new TypeError('str_starts_with(): Argument #2 ($needle) must be of type string, object given'); if (is_resource($needle) ) throw new TypeError('str_starts_with(): Argument #2 ($needle) must be of type string, resource given'); if ((string)$needle === '') return true; return strpos((string)$haystack, (string)$needle) === 0; } } } Test data: str_starts_with('100', '') # true str_starts_with('100', '1') # true str_starts_with('100', '0') # false str_starts_with('100', 0) # false str_starts_with('100', 1) # true str_starts_with('100', 0.0) # false str_starts_with('100', 1.0) # true str_starts_with('100', true) # true / (string)true === '1' str_starts_with('100', false) # true / (string)false === '' str_starts_with('100', null) # Warning str_starts_with('100', []) # Exception str_starts_with('100', [0]) # Exception str_starts_with('100', [1]) # Exception str_starts_with('100', new stdCLass) # Exception str_starts_with('100', new SomeClass) # Exception str_starts_with('100', fopen('resource') # Exception str_starts_with('010', '') # true str_starts_with('010', '1') # false str_starts_with('010', '0') # true str_starts_with('010', 0) # true str_starts_with('010', 1) # false str_starts_with('010', 0.0) # true str_starts_with('010', 1.0) # false str_starts_with('010', true) # false / (string)true === '1' str_starts_with('010', false) # true / (string)false === '' str_starts_with('010', null) # Warning str_starts_with('010', []) # Exception str_starts_with('010', [0]) # Exception str_starts_with('010', [1]) # Exception str_starts_with('010', new stdCLass) # Exception str_starts_with('010', new SomeClass) # Exception str_starts_with('010', fopen('resource') # Exception str_starts_with('', '1') # false str_starts_with('100', '1') # true str_starts_with('010', '1') # false str_starts_with('001', '1') # false str_starts_with(0, '1') # false str_starts_with(1, '1') # true str_starts_with(0.0, '1') # false str_starts_with(1.0, '1') # true str_starts_with(true, '1') # true / (string)true === '1' str_starts_with(false, '1') # false ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 2a00:1760:8006:1c7:f0f8:418f:c179:4305) ---- Manual Page -- https://php.net/manual/en/function.str-starts-with.php Edit -- https://main.php.net/note/edit/128961 Del: integrated -- https://main.php.net/note/delete/128961/integrated Del: useless -- https://main.php.net/note/delete/128961/useless Del: bad code -- https://main.php.net/note/delete/128961/bad+code Del: spam -- https://main.php.net/note/delete/128961/spam Del: non-english -- https://main.php.net/note/delete/128961/non-english Del: in docs -- https://main.php.net/note/delete/128961/in+docs Del: other reasons-- https://main.php.net/note/delete/128961 Reject -- https://main.php.net/note/reject/128961 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220920) next »