从文本字符串中提取第一个匹配项


Extract first match from a string of text?

假设我有以下字符串:

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

我想匹配以下单词:

蓝色,红色,橙色,紫色

因此,它在变量中搜索这 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];

如果我正确理解了你的问题。

区分

大小写:

<?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';                                                                                                                   
} 
?>
$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 );