PHP | fnmatch()函数

PHP中的fnmatch()函数,用于根据指定的模式匹配文件名或字符串。要检查的模式和文件名作为参数发送到fnmatch()函数,如果找到匹配项,则返回True,如果失败,则返回False。 fnmatch()函数现在可用于PHP 5.3.0版本的Windows平台。

null

语法:

fnmatch(pattern, string, flags)

使用的参数: PHP中的fnmatch()函数接受三个参数。

  1. 模式: 它是一个强制参数,指定要搜索的模式。
  2. 字符串: 它是一个强制参数,指定要检查的字符串或文件。
  3. 旗帜: 它是一个可选参数,用于指定标志或标志组合。 这些标志可以是以下标志的组合:
    • FNM_路径名:用于指定字符串中的斜杠,仅与给定模式中的斜杠匹配。
    • FNM_NOESCAPE:用于禁用反斜杠转义。
    • FNM_CASEFOLD:用于无壳火柴。
    • FNM_句点:用于指定字符串中的前导句点必须与给定模式中的句点完全匹配。

返回值: 如果找到匹配项,则返回True;如果失败,则返回False。

错误和例外:

  1. 如果fnmatch()函数被多次使用,则必须清除缓冲区。
  2. fnmatch()函数返回Boolean False,但很多时候,它会返回一个计算结果为False的非布尔值。

下面的程序演示了fnmatch()函数。

方案1 假设有一个名为“gfg.txt”的文件

<?php
$check = "gfg.txt" ;
// fnmatch function used to check
for file starting with g
if ( fnmatch ( "*[g]*" , $check ))
{
echo "gfg" ;
}
else
{
echo "match not found" ;
}
?>


输出:

gfg

方案2

<?php
$check = "GeeksforGeeks" ;
// fnmatch function used to check for
// a word practice or practise
if ( fnmatch ( "*Geeks[gfgj]orGeeks" , $check ))
echo "Yes" ;
else
echo "No" ;
?>


输出:

Yes

方案3

<?php
$check = 'GFG A computer science portal' ;
// fnmatch function used to check
// for a word php without considering its case
if ( fnmatch ( "*[PUTgfg]*" , $check , FNM_CASEFOLD))
echo "Yes" ;
else
echo "No" ;
?>


输出:

Yes

方案4

<?php
$check = "There is a back slash in this sentence" ;
// fnmatch function used to check for a
if ( fnmatch ( "*[]*" , $check , FNM_NOESCAPE))
echo "back slash  ()  in the sentence " ;
else
echo "match not found" ;
?>


输出:

back slash  ()  in the sentence

参考: http://php.net/manual/en/function.fnmatch.php

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享