note 126632 added to function.str-contains

From: Date: Mon, 22 Nov 2021 12:28:11 +0000
Subject: note 126632 added to function.str-contains
Groups: php.notes 
Request: Send a blank email to php-notes+get-217279@lists.php.net to get a copy of this message
Until PHP 8 was released, many-a-programmer were writing our own contain() functions. Mine also handles needles with logical ORs (set to '||'). Here it is. function contains($haystack, $needle, $offset){ $OR = '||'; $result = false; $ORpos = strpos($needle, $OR, 0); if($ORpos !== false){ //ORs exist in the needle string $needle_arr = explode($OR, $needle); for($i=0; $i < count($needle_arr); $i++){ $pos = strpos($haystack, trim($needle_arr[$i]), $offset); if($pos !== false){ $result = true; break; } } } else { $pos = strpos($haystack, trim($needle), $offset); if($pos !== false){ $result = true; } } return($result); } Call: contains("Apple Orange Banana", "Apple || Walnut", 0); Returns: true ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 45.139.213.150) ---- Manual Page -- https://php.net/manual/en/function.str-contains.php Edit -- https://main.php.net/note/edit/126632 Del: integrated -- https://main.php.net/note/delete/126632/integrated Del: useless -- https://main.php.net/note/delete/126632/useless Del: bad code -- https://main.php.net/note/delete/126632/bad+code Del: spam -- https://main.php.net/note/delete/126632/spam Del: non-english -- https://main.php.net/note/delete/126632/non-english Del: in docs -- https://main.php.net/note/delete/126632/in+docs Del: other reasons-- https://main.php.net/note/delete/126632 Reject -- https://main.php.net/note/reject/126632 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217279) next »