将数组值与文本匹配

时间:2012-12-31 16:11:48

标签: php arrays preg-match

  

可能重复:
  How do you perform a preg_match where the pattern is an array, in php?

如何将数组中的值与字符串匹配?

我使用一个数组来修复文本中的拼写错误。我想回收这个数组以匹配它的值。

$fruitNameFixes = array(
        '/bann?ann?a/i' => 'banana',        
        '/app?e?le?/i' => 'apple',
    );

$str = "I like bananas and oranges!";

if( preg_match('/(banana|apple)/i', $str) ) { //add $fruitNameFixes values
    echo "He likes fruits from the fruitNameFixes array";
} else {
    echo "Nope, he doesn't like fruits from the fruitNameFixes array";
}

测试:http://codepad.viper-7.com/N92WVb

0 个答案:

没有答案