是否可以使用通配符匹配文本?如果是,如何使用PHP在文本中查找通配符


Is it possible to use wildcard match with text? if yes how to find wildcard in text using PHP?

我有下面的字符串示例:

许多类型的设备不需要修理它们,他们尝试等等…

我想知道子字符串是否在这个文本中,但使用类似通配符的例子如下:

de*es

因此它将找到"设备"或其他通配符情况像

de*

d*v*s

php怎么能做到这一点?

我试图使用fnmatch函数,但它不起作用!

$txt="many kinds of devices not need repair them and they try etc....";
if (fnmatch("de*es",$text)) { echo "found";} else {exit;}

我使用了下面的代码,它对我来说很有用,但它需要我在通配符的两端加上(*或空格),才能在字符串中进行搜索。

function wildcardcheck( $wildcard, $orginal_text) 
{
    $regex = str_replace(array("'*", "'?"), array('.*','.'), preg_quote($wildcard));
    return preg_match('/^'.$regex.'$/is', $orginal_text);
}
$text="many kinds of devices not need repair them and they try etc...";
$word="de*es*";
if(ltrim($word,'*')==$word) $word=' '.$word; else $word=ltrim($word,'*');
if(rtrim($word,'*')==$word) $word=$word.' '; else $word=rtrim($word,'*');
if (wildcardcheck($word,$text)) { echo "found";} else {echo "not found";}