Source Code - astrstr()

<?php
 
/**
 * function astrstr()
 *
 * Function to find the first instance of any needle from an array of strings 
 * within a string
 * 
 *
 * @author      Stuart Jones <stuart@random-tweak.co.uk>
 * @copyright   Copyright 2008 Stuart Jones
 * @version   08.11.25
 * @license   <a href="http://www.gnu.org/licenses/gpl.html<br />
" title="http://www.gnu.org/licenses/gpl.html<br />
">http://www.gnu.org/licenses/gpl.html<br />
</a> * 
 * @param    string          $haystack
 * @param    array&lt;string&gt;   $needles
 * @param   [int          $offset]
 * @param   [int          $flags]
 */
 
 
// define constants for function flags 
if (!defined('ASTR_NEEDLE_ORDER')) { define('ASTR_NEEDLE_ORDER', 1); }
if (!defined('ASTR_STRING_ORDER')) { define('ASTR_STRING_ORDER', 2); }
if (!defined('ASTR_BEFORE_NEEDLE')) { define('ASTR_BEFORE_NEEDLE', 4); }
if (!defined('ASTR_AFTER_OFFSET')) { define('ASTR_AFTER_OFFSET', 8); }
 
 
 
 
function astrstr($haystack, $needles, $offset = 0, $flags = 0)
{
 
 // some error handling for incoming values 
 $backtrace = debug_backtrace();
 
  if (!is_string($haystack)) {
 
   trigger_error('Invalid argument 1 for ' . __FUNCTION__ . '() 
    - expecting string, called in ' . $backtrace[0]['file'] . ' 
    on line ' . $backtrace[0]['line'] . ' and defined', E_USER_WARNING);
 
    return false;
 
  }
 
  if (empty($haystack)) {
 
    trigger_error('Invalid argument 1 for ' . __FUNCTION__ . '() 
    - cannot be an empty string, called in ' . $backtrace[0]['file'] . ' 
   on line ' . $backtrace[0]['line'] . ' and defined', E_USER_WARNING);
 
    return false;
 
  }
 
 
 
  if (!is_array($needles)) {
 
   trigger_error('Invalid argument 2 for ' . __FUNCTION__ . '() 
    - expecting array, called in ' . $backtrace[0]['file'] . ' 
   on line ' . $backtrace[0]['line'] . ' and defined', E_USER_WARNING);
 
    return false;
 
  }
 
  if (empty($needles)) {
 
   trigger_error('Invalid argument 2 for ' . __FUNCTION__ . '() 
    - cannot be an empty array, called in ' . $backtrace[0]['file'] . ' 
    on line ' . $backtrace[0]['line'] . ' and defined', E_USER_WARNING);
 
    return false;
 
  }
 
 
 
  if (!is_int($offset) || $offset < 0) {
 
   trigger_error('Invalid argument 3 for ' . __FUNCTION__ . '() 
    - expecting positive integer, called in ' . $backtrace[0]['file'] . ' 
    on line ' . $backtrace[0]['line'] . ' and defined', E_USER_WARNING);
 
    return false;
 
  }
 
 
 
  if (!is_int($flags) || $flags < 0) {
 
   trigger_error('Invalid argument 3 for ' . __FUNCTION__ . '() 
    - expecting positive integer, called in ' . $backtrace[0]['file'] . ' 
    on line ' . $backtrace[0]['line'] . ' and defined', E_USER_WARNING);
 
    return false;
 
  }
 
  // the bit that does the work 
  $astrstr = false;
 $pos = false;
 
  foreach ($needles as $needle) {
   // cycle through the needles
 
   $strpos = false;
 
   if (($strpos = strpos($haystack, $needle, $offset)) !== false) {
 
     if ($flags && ASTR_NEEDLE_ORDER) {
 
       $pos = $strpos;
       break;
 
     } 
 
     if (($pos > $strpos) || ($pos === false)) {
 
        $pos = $strpos;  
 
      }
 
    }
 
  }
 
  if ($pos !== false) {
 
    if ($flags && ASTR_BEFORE_NEEDLE) {
 
      $astrstr = substr($haystack, 0, $pos);
 
   } elseif ($flags && ASTR_AFTER_OFFSET) {
 
     $astrstr = substr($haystack, $offset, $pos);
 
   } else {
 
     $astrstr = substr($haystack, $pos);
 
    }
 
  } 
 
 return $astrstr;
 
}
 
?>

Licence

The program/snippet/script provided in this post is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.