strposa

Example: strposa ( string $haystack, array $needle [, int $offset = 0 ] )

This function searches a string for every element/value found in an array and returns the position of the first element/value found in the string. In technical terms, it searches for mutilple needles in a haystack and returns the position of the first occurance found in the string. Note: this function is case sensitive.


/**
 * strposa ( string $haystack, array $needle [, int $offset = 0 ] )
 *
 * @param string $haystack
 * @param mixed $needles - should be an array, but can be any
 * 						   string, int, or array
 * @param integer $offset
 * @return the position of the first items found in the array
 *
 * Original contibutions by:
 * ==============================
 * 	- Leonardo Martinez ( http://www.leonardomartinez.com/contact/ )
 *	- vbracco at gmail dot com
 *	- nathan at bitmesh dot com
 *	- dpharris at llu dot edu
 *
 * Description:
 * ========================
 *  - searches for mutilple needles in a haystack and returns
 *	  the position of the first occurance, case-sensitive.
 *  - finds the position of the first occurrence of array (needles)
 *    in the haystack string, case-sensitive.
 *  - if you are looking for more than one needle in the haystack,
 * 	  this function will replace php's strpos()
 *  - if you searching for the position of a single value, it is
 *    recommended that you use strpos().
 *
 *
 * Notes:
 * ====================================
 * below are several functions that search for the position of a needle)
 * ---------------------------------------------------------------------------
 * 	strpos()	- Returns the position of the first occurrence of a needle
 * 				  in the haystack string, case-sensitive.
 * 	stripos() 	- Returns the position of the first occurrence of a needle
 * 				  in the haystack string, case-insensitive.
 *  strrpos()	- Returns the position of the last occurrence of needle in
 * 				  the haystack string, case-sensitive.  Note that the needle
 * 				  in this case can only be a single character in PHP 4. If a
 *  			  string is passed as the needle, then only the first character
 * 				  of that string will be used.
 * 	strripos() 	- Returns the position of the last occurrence of needle in the
 * 			  	  haystack string, case-insensitive.
 *
 */

function strposa( $haystack, $needles, $offset=0 ){

	if (!is_array( $needles )) $needles = array( $needles );

	$result = array();
    foreach( $needles as $needle ){
        if ( strpos( $haystack, $needle, $offset) !== false ) {
          $result[] = strpos( $haystack, $needle, $offset);
       }
    }

    if( empty( $result )) return false;

    return min( $result );

}

$haystack = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood';
$needles = array('woodchuck', 'wood', 'could', 'chuck');

$string = strposa( $haystack, $needles );
print_r($string);

/**
 * the result will be 9,
 * it found the word 'wood' first on the 9th element of the haystack.
 * remember that it's really the 10th item on the list, but php starts with 0 (zero).
 */

Comments are closed.