有谁知道如何编写regex模式,它是这样做的:
假设我在数组中有这样的字母
$letters = array('a','b','a');我们还有一个单词Alabama,我希望preg_match返回true,因为它包含字母A和B两次,但是对于单词Ab,它应该返回false,因为这个单词中没有两个A。
有什么想法吗?
编辑:我尝试的唯一模式是a,b,a,但它对包含这些字母中的一个的每个单词都返回true,并且不会检查出现多个字母的情况
发布于 2016-11-21 15:01:32
我认为你没有必要把这个过程搞得过于复杂。您可以遍历letters并检查word中是否存在,如果所有字母都存在,则返回true。如下所示:
$letters = array('a','b','a');
$word = "Alabama";
function inWord($l,$w){
//For each letter
foreach($l as $letter){
//check if the letter is in the word
if(($p = stripos($w,$letter)) === FALSE)
//if false, return false
return FALSE;
else
//if the letter is there, remove it and move to the next one
$w = substr_replace($w,'',$p,1);
}
//If it found all the letters, return true
return TRUE;
}并像这样使用它:inWord($letters,$word);
请注意,这是不区分大小写的,如果您需要它,请将stripos替换为strpos
https://stackoverflow.com/questions/40713495
复制相似问题