假设 String 是一个折叠/分隔的值列表
function arrayInString( $inArray , $inString , $inDelim=',' ){
$inStringAsArray = explode( $inDelim , $inString );
return ( count( array_intersect( $inArray , $inStringAsArray ) )>0 );
}
示例 1:
arrayInString( array( 'red' , 'blue' ) , 'red,white,orange' , ',' );
// Would return true
// When 'red,white,orange' are split by ',',
// the 'red' element matched the array
示例 2:
arrayInString( array( 'mouse' , 'cat' ) , 'mouse' );
// Would return true
// When 'mouse' is split by ',' (the default deliminator),
// the 'mouse' element matches the array which contains only 'mouse'
假设 String 是纯文本,并且您只是在其中查找指定单词的实例
function arrayInString( $inArray , $inString ){
if( is_array( $inArray ) ){
foreach( $inArray as $e ){
if( strpos( $inString , $e )!==false )
return true;
}
return false;
}else{
return ( strpos( $inString , $inArray )!==false );
}
}
示例 1:
arrayInString( array( 'apple' , 'banana' ) , 'I ate an apple' );
// Would return true
// As 'I ate an apple' contains 'apple'
示例 2:
arrayInString( array( 'car' , 'bus' ) , 'I was busy' );
// Would return true
// As 'bus' is present in the string, even though it is part of 'busy'