是否有可能在PHP正则表达式来进行部分匹配,这样,如果我有这样一个数组:正则表达式部分字符串匹配
$words = array("john", "steve", "amie", "kristy", "steven");
我提供“乔”,它会返回“约翰”或者“EVE”被提供,它返回“史蒂夫”?
是否有可能在PHP正则表达式来进行部分匹配,这样,如果我有这样一个数组:正则表达式部分字符串匹配
$words = array("john", "steve", "amie", "kristy", "steven");
我提供“乔”,它会返回“约翰”或者“EVE”被提供,它返回“史蒂夫”?
$words = array("john","jogi", "steve", "amie", "kristy", "steven");
foreach ($words as $value) {
if (preg_match("|jo|", $value)) {
$arr[] = $value;
}
}
var_dump($arr);
这将返回与约翰和乔吉
你可以遍历你的数组,并使用preg_match来匹配你的项目。
如果您不提供^
和$
它会自动进行部分匹配。
为什么你的正则表达式数组?为什么不'strpos(“john”,“jo”)'? – 2012-02-10 08:19:22
,因为当提供搜索词时,它应该返回最佳匹配。 strpos要求我知道要对比的单词 – dukevin 2012-02-10 08:20:43
你刚刚说过要比较的单词是'jo'。所以迭代数组并查看'strpos()'不返回'False'。我没有看到问题。 – 2012-02-10 08:21:47