1

假设我有以下字符串:

你好,我的车是红色的,我的鞋是蓝色的

我想匹配以下单词:

蓝色, 红色, 橙色, 紫色

所以它在变量中搜索这 4 个词,并返回第一个词——在我的示例中,返回的词是“红色”。但是如果汽车是蓝色的,那么首先会返回单词 blue,因为它是第一个找到可能匹配列表的单词。

我怎样才能做到这一点?

4

3 回答 3

4
$str  = 'hello my car is red and my shoe is blue';
$find = 'blue,red,orange,purple';
$pattern = str_replace(',','|',$find);
preg_match('#'.$pattern.'#i',$str,$match);
echo $match[0];

如果我正确理解了您的问题。:-)

于 2012-07-03T11:32:27.397 回答
0

区分大小写 :

<?php
$subject = "hello my car is blue and my shoe is blue";                                                                                      
$pattern = '/blue|red|orange|purple/';                                                                                                      
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);                                                                              
if (!empty($matches)) {                                                                                                                     
  echo 'Matched `' . $matches[0][0] . '` at index `' . $matches[0][1] . '`';                                                                
} else {                                                                                                                                    
  echo 'Nothing matched';                                                                                                                   
} 
?>

不区分大小写 :

<?php
$subject = "hello my car is blue and my shoe is blue";                                                                                      
$pattern = '/blue|red|orange|purple/';                                                                                                      
preg_match(strtolower($pattern), strtolower($subject), $matches, PREG_OFFSET_CAPTURE);                                                                              
if (!empty($matches)) {                                                                                                                     
  echo 'Matched `' . $matches[0][0] . '` at index `' . $matches[0][1] . '`';                                                                
} else {                                                                                                                                    
  echo 'Nothing matched';                                                                                                                   
} 
?>
于 2012-07-03T11:35:14.670 回答
0
$string = 'hello my car is red and my shoe is blue';
$words = array ( 'blue', 'red', 'orange', 'purple' );

function checkForWords ( $a, $b ) {

    $pos = 0;
    $first = 0;
    $new_word = '';

    foreach ( $b as $value ) {
        $pos = strpos( $a, $value );

        # First match
        if ( !$first && $pos ) {
            $new_word = $value;
            $first = $pos;          
        }

        # Better match
        if ( $pos && ( $pos < $first ) ) {
            $new_word = $value;
            $first = $pos;
        }
    }
    return $new_word;

}

echo checkForWords ( $string, $words );
于 2012-07-03T11:38:26.683 回答