当前位置:   article > 正文

php检测字符串在数组中存在,php – 检查字符串中是否存在数组元素

php 判断多个字符串串是否出现在数组中

假设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'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/634224
推荐阅读
相关标签
  

闽ICP备14008679号