PHP 8.4.24 Released!

Voting

: min(three, four)?
(Example: nine)

The Note You're Voting On

m.m.j.kronenburg
9 years ago
<?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;
}

?>

<< Back to user notes page

To Top