


PHP is suitable for windows fnmatch (matching function), which can match Chinese. _PHP Tutorial
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; }
if (!function_exists('fnmatch')) { function fnmatch($pattern, $string) { return @preg_match('/^' . strtr(addcslashes($pattern, '.+^$(){}=!<>|'), array('*' => '.*', '?' => '.?')) . '$/i', $string); } }
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; }
After the implementation is completed, it can match Chinese perfectly.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use