Home Backend Development PHP Tutorial PHP is suitable for windows fnmatch (matching function), which can match Chinese. _PHP Tutorial

PHP is suitable for windows fnmatch (matching function), which can match Chinese. _PHP Tutorial

Jul 13, 2016 pm 05:18 PM
windows

There are two ways to implement the fnmatch function in this post. The current post is as follows:

function fnmatch($pattern, $string)         //$pattern匹配式, $string被匹配的字符串
{
    $starStack = array();                   //创建记录pattern开始位置的栈,这个作用是像编辑器的后退
    $sstrStack = array();                   //创建记录$string开始位置的栈
    $countStack = 0;                        //栈大小,用一个同步记录栈大小,减少count()时所耗的时间
    $ptnStart = strlen($pattern) - 1;       //定位匹配式最后一个字符, 算法是从字符串后面开始匹配
    $strStart = strlen($string) - 1;        //定位字符串的最好一个字符
    for(; 0 <= $strStart; $strStart --)     //开始匹配循环, 每匹配一个字符, $strStart就往前移一个字符
    {
        $sc = $string{$strStart};           //取得当前在比较的字符
        $pc = ($ptnStart < 0) ? '' : $pattern{$ptnStart};//取得匹配式当前的字符,已到结束位置,给个空
        if($sc !== $pc)
        {                                   //当两个字符不相同时, 就要进行一些匹配式特殊字符的比较
            if($pc === '*')                 //如果匹配式当前字符是*号, 进行*号匹配
            {
                while($ptnStart > 0 && ($pc = $pattern{$ptnStart - 1}) === '*')
                    $ptnStart --;           //while这段是去除几个连续的*号, 并尝试和取得下一个字符
                if($ptnStart > 0 && ($pc === $sc || $pc === '?'))//比较下个字符是否相同或是?号
                {                           //如果下一个字符匹配成功
                    $starStack[$countStack] = $ptnStart;//保存这个*号的位置
                    $sstrStack[$countStack] = $strStart;//保存$string开始位置
                    $countStack ++;         //栈向下移一
                    $ptnStart -= 2;         //匹配式定位,前移两位,分别是当前*号位和已经匹配的一个
                    continue;               //进行下一次循环
                }
            }
            elseif($pc === '?')             //如果匹配式当前字符是?号, 进行?号匹配
            {
                $ptnStart --;               //?号匹配是字符串同步前移一个位置
            }
            elseif($countStack > 0)         //如果不是通配符,检查栈中是否有保存上一个*号的位置
            {                               //有就还原此*号位置, 回到上一个*号处再次进行匹配
                $countStack --;
                $ptnStart = $starStack[$countStack];//还原*号位置
                $strStart = $sstrStack[$countStack];//还原$string开始位置
            }
            else 
            {
                return false;               //以上情况都没有的话, 匹配失败, 返回flase
            }
        }
        else
        {
            $ptnStart --;                   //字符串位置和匹配式位置上相同,前移一位,继续下个匹配
        }
    }                                       //匹配循环结束
    if($ptnStart === -1)                    //刚好匹配式的位置也结束, 则匹配成功, 返回true
    {
        return true;
    }
    elseif($ptnStart >= 0)                  //匹配式并没有结束, 还有一些没有匹配
    {
        while($ptnStart > 0 && $pattern{$ptnStart} === '*')//检查剩下的是不是都是*号,去除这些*号
            $ptnStart --;
        if($pattern{$ptnStart} === '*')     //最后的只有一个*号结束的话, 就匹配成功, 返回true
            return true;
        else
            return false;                   //否则, 返回false
    }
    return false;
}
Copy after login

if (!function_exists('fnmatch')) {
        function fnmatch($pattern, $string) {
            return @preg_match('/^' . strtr(addcslashes($pattern, '.+^$(){}=!<>|'), array('*' => '.*', '?' => '.?')) . '$/i', $string);
        }
    }
Copy after login

Both of these methods can be implemented, but since the ones I want to match include Chinese characters, such as

I love China

Match I love??

cannot be realized, because the character "China" counts as 4 characters. If it matches I love ????, it should be no problem, but it is very inconvenient for us to use, so I changed it. The implementation of the first function uses the mb_strlen method to count and segment characters. The implementation is as follows:

function fnmatch($pattern, $string)         //$pattern匹配式, $string被匹配的字符串
{
	$encoding = gb2312;					//根据自己的页面的编码,来定义这个编码
    $starStack = array();                   //创建记录pattern开始位置的栈,这个作用是像编辑器的后退
    $sstrStack = array();                   //创建记录$string开始位置的栈
    $countStack = 0;                        //栈大小,用一个同步记录栈大小,减少count()时所耗的时间
    $ptnStart = mb_strlen($pattern, $encoding) - 1;       //定位匹配式最后一个字符, 算法是从字符串后面开始匹配
    $strStart = mb_strlen($string, $encoding) - 1;        //定位字符串的最好一个字符
    for(; 0 <= $strStart; $strStart --)     //开始匹配循环, 每匹配一个字符, $strStart就往前移一个字符
    {
		$sc = mb_substr($string, $strStart, 1, $encoding);           //取得当前在比较的字符
		$pc = ($ptnStart < 0) ? '' : mb_substr($pattern, $ptnStart, 1, $encoding);//取得匹配式当前的字符,已到结束位置,给个空
        if($sc !== $pc)
        {                                   //当两个字符不相同时, 就要进行一些匹配式特殊字符的比较
            if($pc === '*')                 //如果匹配式当前字符是*号, 进行*号匹配
            {
                while($ptnStart > 0 && ($pc = mb_substr($pattern, $ptnStart-1, 1, $encoding)) === '*')
                    $ptnStart --;           //while这段是去除几个连续的*号, 并尝试和取得下一个字符
                if($ptnStart > 0 && ($pc === $sc || $pc === '?'))//比较下个字符是否相同或是?号
                {                           //如果下一个字符匹配成功
                    $starStack[$countStack] = $ptnStart;//保存这个*号的位置
                    $sstrStack[$countStack] = $strStart;//保存$string开始位置
                    $countStack ++;         //栈向下移一
                    $ptnStart -= 2;         //匹配式定位,前移两位,分别是当前*号位和已经匹配的一个
                    continue;               //进行下一次循环
                }
            }
            elseif($pc === '?')             //如果匹配式当前字符是?号, 进行?号匹配
            {
                $ptnStart --;               //?号匹配是字符串同步前移一个位置
            }
            elseif($countStack > 0)         //如果不是通配符,检查栈中是否有保存上一个*号的位置
            {                               //有就还原此*号位置, 回到上一个*号处再次进行匹配
                $countStack --;
                $ptnStart = $starStack[$countStack];//还原*号位置
                $strStart = $sstrStack[$countStack];//还原$string开始位置
            }
            else 
            {
                return false;               //以上情况都没有的话, 匹配失败, 返回flase
            }
        }
        else
        {
            $ptnStart --;                   //字符串位置和匹配式位置上相同,前移一位,继续下个匹配
        }
    }                                       //匹配循环结束
    if($ptnStart === -1)                    //刚好匹配式的位置也结束, 则匹配成功, 返回true
    {
        return true;
    }
    elseif($ptnStart >= 0)                  //匹配式并没有结束, 还有一些没有匹配
    {
        while($ptnStart > 0 && mb_substr($pattern, $ptnStart, 1, $encoding) === '*')//检查剩下的是不是都是*号,去除这些*号
            $ptnStart --;
        if(mb_substr($pattern, $ptnStart, 1, $encoding) === '*')     //最后的只有一个*号结束的话, 就匹配成功, 返回true
            return true;
        else
            return false;                   //否则, 返回false
    }
    return false;
}
Copy after login

After the implementation is completed, it can match Chinese perfectly.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621633.htmlTechArticleThere are two ways to implement the fnmatch function in this post. The current post is as follows: function fnmatch($pattern, $string ) //$pattern matching formula, $string matched string { $starStack = array(); // Create...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Lossless Scaling on Steam Deck OLED runs games at up to 2x FPS Lossless Scaling on Steam Deck OLED runs games at up to 2x FPS Aug 26, 2024 am 10:07 AM

Lossless Scaling on Steam Deck OLED runs games at up to 2x FPS

What should I do if I can't delete a Windows system dll file? Tips for completely deleting stubborn dll files What should I do if I can't delete a Windows system dll file? Tips for completely deleting stubborn dll files Jun 12, 2024 pm 02:46 PM

What should I do if I can't delete a Windows system dll file? Tips for completely deleting stubborn dll files

How to turn off Security Center in Windows 11 How to turn off Security Center in Windows 11 Mar 28, 2024 am 10:21 AM

How to turn off Security Center in Windows 11

Windows 10 vs. Windows 11 performance comparison: Which one is better? Windows 10 vs. Windows 11 performance comparison: Which one is better? Mar 28, 2024 am 09:00 AM

Windows 10 vs. Windows 11 performance comparison: Which one is better?

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

deepseek web version entrance deepseek official website entrance

How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? Feb 21, 2025 pm 10:54 PM

How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated?

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

How to install deepseek

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node?

See all articles