Source code: Function astrpos()
<?php /** * function astrpos() * * Function to find the position of 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.10.13 * @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<string> $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); } function astrpos($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 $astrpos = false; foreach ($needles as $needle) { // cycle through the needles $strpos = false; if (($strpos = strpos($haystack, $needle, $offset)) !== false) { if ($flags && ASTR_NEEDLE_ORDER) { return $strpos; } if (($astrpos > $strpos) || ($astrpos === false)) { $astrpos = $strpos; } } } return $astrpos; } ?>
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.