Created
July 18, 2012 07:09
-
-
Save ginsterbusch/3134730 to your computer and use it in GitHub Desktop.
Replacement for in_array (for string searches only)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Fabian Wolf | |
* @version 0.2 | |
* @license LGPL | |
* @link http://usability-idealist.de/ | |
*/ | |
if( !function_exists('str_in_array') ) { | |
function str_in_array( $string, $array, $ignore_case = false ) { | |
$return = false; | |
if( !is_string( $string ) ) { // try to convert given value to string | |
switch( gettype($string) ) { | |
case 'integer': | |
case 'double': | |
case 'float': | |
$string = (string) "$string"; | |
break; | |
case 'boolean': | |
$string = (string) ($string === false ? '1' : '0'); | |
break; | |
} | |
} | |
if( is_string( $string) != false && is_array($array) != false && !empty($array) && !empty($string) ) { | |
$needle = $string; | |
$haystack = implode(' ', $array); | |
$result = ( $ignore_case != false ? stripos( $haystack, $needle) : strpos( $haystack, $needle ) ); | |
if($result !== false) { | |
$return = true; | |
} | |
} | |
return $return; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment