Skip to content

Instantly share code, notes, and snippets.

@ginsterbusch
Created July 18, 2012 07:09
Show Gist options
  • Save ginsterbusch/3134730 to your computer and use it in GitHub Desktop.
Save ginsterbusch/3134730 to your computer and use it in GitHub Desktop.
Replacement for in_array (for string searches only)
<?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