字符串组合匹配

The*_*art 4 php string

假设一个字符串:

$str = 'a_b_c';

我希望匹配所有可能的组合a, b, c与上面.例如:

b_a_c,c_a_b,a_c_b...等会给予true当上述比较$str.

注意:

$str可能是随机的.如:a_b,k_l_m_n等

Bra*_*rad 7

我会将您的字符串拆分为一个数组,然后将其与要匹配的元素数组进行比较.

$originalList = explode('_', 'a_b_c');
$matchList = array('a', 'b', 'c');
$diff = array_diff($matchList, $originalList);
if (!empty($diff)) {
    // At least one of the elements in $matchList is not in $originalList
}
Run Code Online (Sandbox Code Playgroud)

请注意重复元素和不重复元素,具体取决于数据的来源.

文档: