note 119853 added to function.strpos

From: Date: Tue, 06 Sep 2016 22:56:02 +0000
Subject: note 119853 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-206512@lists.php.net to get a copy of this message
<?php /** * Find the position of the first occurrence of one or more substrings in a * string. * * This function is simulair to function strpos() except that it allows to * search for multiple needles at once. * * @param string $haystack The string to search in. * @param mixed $needles Array containing needles or string containing * needle. * @param integer $offset If specified, search will start this number of * characters counted from the beginning of the * string. * @param boolean $last If TRUE then the farthest position from the start * of one of the needles is returned. * If FALSE then the smallest position from start of * one of the needles is returned. **/ function mstrpos($haystack, $needles, $offset = 0, $last = false) { if(!is_array($needles)) { $needles = array($needles); } $found = false; foreach($needles as $needle) { $position = strpos($haystack, (string)$needle, $offset); if($position === false) { continue; } $exp = $last ? ($found === false || $position > $found) : ($found === false || $position < $found); if($exp) { $found = $position; } } return $found; } /** * Find the position of the first (partially) occurrence of a substring in a * string. * * This function is simulair to function strpos() except that it wil return a * position when the substring is partially located at the end of the string. * * @param string $haystack The string to search in. * @param mixed $needle The needle to search for. * @param integer $offset If specified, search will start this number of * characters counted from the beginning of the * string. **/ function pstrpos($haystack, $needle, $offset = 0) { $position = strpos($haystack, $needle, $offset); if($position !== false) { return $position; } for($i = strlen($needle); $i > 0; $i--) { if(substr($needle, 0, $i) == substr($haystack, -$i)) { return strlen($haystack) - $i; } } return false; } /** * Find the position of the first (partially) occurrence of one or more * substrings in a string. * * This function is simulair to function strpos() except that it allows to * search for multiple needles at once and it wil return a position when one of * the substrings is partially located at the end of the string. * * @param string $haystack The string to search in. * @param mixed $needles Array containing needles or string containing * needle. * @param integer $offset If specified, search will start this number of * characters counted from the beginning of the * string. * @param boolean $last If TRUE then the farthest position from the start * of one of the needles is returned. * If FALSE then the smallest position from start of * one of the needles is returned. **/ function mpstrpos($haystack, $needles, $offset = 0, $last = false) { if(!is_array($needles)) { $needles = array($needles); } $found = false; foreach($needles as $needle) { $position = pstrpos($haystack, (string)$needle, $offset); if($position === false) { continue; } $exp = $last ? ($found === false || $position > $found) : ($found === false || $position < $found); if($exp) { $found = $position; } } return $found; } ?> ---- Server IP: 83.137.20.135 Probable Submitter: 141.105.0.254 ---- Manual Page -- http://php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/119853 Del: integrated -- https://master.php.net/note/delete/119853/integrated Del: useless -- https://master.php.net/note/delete/119853/useless Del: bad code -- https://master.php.net/note/delete/119853/bad+code Del: spam -- https://master.php.net/note/delete/119853/spam Del: non-english -- https://master.php.net/note/delete/119853/non-english Del: in docs -- https://master.php.net/note/delete/119853/in+docs Del: other reasons-- https://master.php.net/note/delete/119853 Reject -- https://master.php.net/note/reject/119853 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#206512) next »