Php is applicable to fnmatch (matching function) in windows and can match Chinese characters.

Source: Internet
Author: User

 

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

 

Function fnmatch ($ pattern, $ string) // $ pattern match, $ string matched string {$ starStack = array (); // create a stack at the starting position of the pattern record, this function is like the back of the editor $ sstrStack = array (); // create the stack at the starting position of $ string $ countStack = 0; // stack size, use a synchronous record stack size to reduce the time spent on count () $ ptnStart = strlen ($ pattern)-1; // locate the last character of the matching expression, the algorithm is to match $ strStart = strlen ($ string)-1 from the end of the string; // locate the best character of the string for (; 0 <= $ strStart; $ strStart --) // start the matching loop. Every time a character is matched, $ strStart will move forward one character {$ SC = $ string {$ strStart}; // get the current comparison character $ pc = ($ ptnStart <0 )? '': $ Pattern {$ ptnStart}; // gets the current matching character. It has reached the end position, and an empty if ($ SC! ==$ Pc) {// when the two characters are not the same, we need to compare some matching special characters if ($ pc = '*') // if the current character of the matching expression is *, perform * matching {while ($ ptnStart> 0 & ($ pc = $ pattern {$ ptnStart-1 }) === '*') $ ptnStart --; // while is to remove several consecutive * numbers, and try and get the next character if ($ ptnStart> 0 & ($ pc ===$ SC | $ pc === '? ') // Compare whether the next character is the same or? # {// If the next character matches successfully $ starStack [$ countStack] = $ ptnStart; // location where the * number is saved $ sstrStack [$ countStack] = $ strStart; // save $ string start position $ countStack ++; // stack moves down one $ ptnStart-= 2; // matches the position and moves two places forward, is the current * number and the matched continue; // perform the next loop} elseif ($ pc = '? ') // If the current character of the matching expression is?? Matched {$ ptnStart --;//? Match Number is a place where the string is synchronized forward} elseif ($ countStack> 0) // if it is not a wildcard, check whether the position of the last * number is saved in the stack {// If yes, restore the position of this * Number and return to the last * number for matching again $ countStack --; $ ptnStart = $ starStack [$ countStack]; // restore * position $ strStart = $ sstrStack [$ countStack]; // restore $ string start position} else {return false; // If none of the above conditions exists, the match fails. flase} else {$ ptnStart --; // The string position is the same as the matching position, and the string is moved one by one, continue the next match} // The matching loop ends. if ($ ptnStart =-1) // if the position of the matching formula also ends, the matching succeeds, returns true {return true;} elseif ($ ptnStart> = 0) // The matching formula is not complete, others do not match {while ($ ptnStart> 0 & $ pattern {$ ptnStart} = '*') // check whether the remaining number is, remove the * signs $ ptnStart --; if ($ pattern {$ ptnStart} = '*') // if only one * sign ends, the match is successful, return true; else return false; // otherwise, return 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 because I want to match Chinese, such

 

I love China

Match my love ??

It cannot be implemented because the "China" character is counted as four characters. If it matches my love ???? It should be okay, but this is very inconvenient for us to use. So I changed the implementation of the first function and used the mb_strlen method to count and split characters. The implementation is as follows:

 

Function fnmatch ($ pattern, $ string) // $ pattern match, $ string matched string {$ encoding = gb2312; // according to the encoding of your own page, to define the code $ starStack = array (); // create the stack at the starting position of the pattern record. This function is like the back of the editor $ sstrStack = array (); // create the stack where the record $ string starts $ countStack = 0; // stack size, use a synchronous record stack size to reduce the count () time spent $ ptnStart = mb_strlen ($ pattern, $ encoding)-1; // locate the last character of the matching expression, the algorithm starts matching $ strStart = mb_strlen ($ string, $ encoding)-1 from the end of the string; // locates the best character of the string for (; 0 <= $ StrStart; $ strStart --) // starts the matching loop. Every time a character is matched, $ strStart moves forward to a character {$ SC = mb_substr ($ string, $ strStart, 1, $ encoding); // get the current comparison character $ pc = ($ ptnStart <0 )? '': Mb_substr ($ pattern, $ ptnStart, 1, $ encoding); // obtain the current character of the matching expression. The end position is reached and an empty if ($ SC! ==$ Pc) {// when the two characters are not the same, we need to compare some matching special characters if ($ pc = '*') // if the current character of the matching expression is *, perform * matching {while ($ ptnStart> 0 & ($ pc = mb_substr ($ pattern, $ ptnStart-1, 1, $ encoding) === '*') $ ptnStart --; // while is used to remove several consecutive * numbers, and try and get the next character if ($ ptnStart> 0 & ($ pc ===$ SC | $ pc === '? ') // Compare whether the next character is the same or? # {// If the next character matches successfully $ starStack [$ countStack] = $ ptnStart; // location where the * number is saved $ sstrStack [$ countStack] = $ strStart; // save $ string start position $ countStack ++; // stack moves down one $ ptnStart-= 2; // matches the position and moves two places forward, is the current * number and the matched continue; // perform the next loop} elseif ($ pc = '? ') // If the current character of the matching expression is?? Matched {$ ptnStart --;//? Match Number is a place where the string is synchronized forward} elseif ($ countStack> 0) // if it is not a wildcard, check whether the position of the last * number is saved in the stack {// If yes, restore the position of this * Number and return to the last * number for matching again $ countStack --; $ ptnStart = $ starStack [$ countStack]; // restore * position $ strStart = $ sstrStack [$ countStack]; // restore $ string start position} else {return false; // If none of the above conditions exists, the match fails. flase} else {$ ptnStart --; // The string position is the same as the matching position, and the string is moved one by one, continue the next match} // The matching loop ends. if ($ ptnStart =-1) // if the position of the matching formula also ends, the matching succeeds, returns true {return true;} elseif ($ ptnStart> = 0) // The matching formula is not complete, some do not match {while ($ ptnStart> 0 & mb_substr ($ pattern, $ ptnStart, 1, $ encoding) === '*') // check whether all the remaining signs are "*". Remove these "*" signs $ ptnStart --; if (mb_substr ($ pattern, $ ptnStart, 1, $ encoding) === '*') // if only one end * ends, the matching succeeds. true return true; else return false; // otherwise, false} return false;} is returned ;}

After implementation, the Chinese character can be perfectly matched.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.