4

これは、ネイティブの php 関数を使用する簡単なことだと思いましたが、人々がそれを達成しようとしたいくつかの異なる、すべて非常に複雑な方法を見つけました。文字列に配列内の 1 つ以上の要素が含まれているかどうかを確認する最も効率的な方法は何ですか? つまり、以下 - $data['description'] は文字列です。param 2 が配列であることを想定しているため、以下の in_array チェックが壊れていることがわかります

$keywords = array(
            'bus',
            'buses',
            'train',
    );

    if (!in_array($keywords, $data['description']))
            continue;
4

3 に答える 3

8

文字列が折りたたまれた/区切られた値のリストであると仮定します

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'

文字列がプレーン テキストであり、その中の指定された単語のインスタンスを単に探していると仮定します。

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'
于 2012-04-13T08:42:11.337 に答える
3

正規表現を使用してこれを行うことができます-

if( !preg_match( '/(\b' . implode( '\b|\b', $keywords ) . '\b)/i', $data['description'] )) continue;

結果の正規表現は次のようになります/(\bbus\b|\bbuses\b|\btrain\b)/

于 2012-04-13T08:41:10.383 に答える
0

単語やフレーズ全体が検索されていると仮定して

この関数は、文字列内のフレーズの配列から (大文字と小文字を区別しない) フレーズを見つけます。見つかった場合、フレーズが返され、$position は文字列内のインデックスを返します。見つからない場合は FALSE を返します。

function findStringFromArray($phrases, $string, &$position) {
    // Reverse sort phrases according to length.
    // This ensures that 'taxi' isn't found when 'taxi cab' exists in the string.
    usort($phrases, create_function('$a,$b',
                                    '$diff=strlen($b)-strlen($a);
                                     return $diff<0?-1:($diff>0?1:0);'));

    // Pad-out the string and convert it to lower-case
    $string = ' '.strtolower($string).' ';

    // Find the phrase
    foreach ($phrases as $key => $value) {
        if (($position = strpos($string, ' '.strtolower($value).' ')) !== FALSE) {
            return $phrases[$key];
        }
    }

    // Not found
    return FALSE;
}

機能をテストするには、

$wordsAndPhrases = array('taxi', 'bus', 'taxi cab', 'truck', 'coach');
$srch = "The taxi cab was waiting";

if (($found = findStringFromArray($wordsAndPhrases, $srch, $pos)) !== FALSE) {
    echo "'$found' was found in '$srch' at string position $pos.";
}
else {
    echo "None of the search phrases were found in '$srch'.";
}

興味深いことに、この関数は単語やフレーズ全体を検出する手法を示しているため、"bus" は検出されますが、"abuse" は検出されません。干し草の山と針の両方をスペースで囲むだけです。

$pos = strpos(" $haystack ", " $needle ")
于 2013-07-21T07:26:28.120 に答える