PHPsimilar_text (), levenshtein (), and lcs () support Chinese character edition, _ PHP Tutorial

Source: Internet
Author: User
PHPsimilar_text (), levenshtein (), and lcs () support Chinese characters ,. PHPsimilar_text (), levenshtein (), and lcs () support Chinese characters. PHP native similar_text () and levenshtein () functions do not support Chinese characters, I wrote a simila PHP similar_text (), levenshtein (), and lcs () that support Chinese characters,

The native similar_text () function and levenshtein () function of PHP do not support Chinese characters. I wrote


Similar_text () Chinese character edition

1
 

Levenshtein () Chinese character edition

1
  $ D [$ mb_len1] [$ mb_len2], 'Count _ same_letter '=> $ count_same_letter); 52}

Longest common subsequence LCS ()

1
  $ Len_2? $ Len_1: $ len_2; 7 8 $ dp = array (); 9 for ($ I = 0; $ I <= $ len; $ I ++) {10 $ dp [$ I] = array (); 11 $ dp [$ I] [0] = 0; 12 $ dp [0] [$ I] = 0; 13} 14 15 for ($ I = 1; $ I <= $ len_1; $ I ++) {16 for ($ j = 1; $ j <= $ len_2; $ j ++) {17 if ($ str_1 [$ I-1] ==$ str_2 [$ j-1]) {18 $ dp [$ I] [$ j] = $ dp [$ I-1] [$ j-1] + 1; 19} else {20 $ dp [$ I] [$ j] = $ dp [$ I-1] [$ j]> $ dp [$ I] [$ j-1 ]? $ Dp [$ I-1] [$ j]: $ dp [$ I] [$ j-1]; 21} 22} 23} 24 25 return $ dp [$ len_1] [$ len_2]; 26} 27 28 // Split string 29 function mbStringToArray ($ string, $ encoding = 'utf-8') {30 $ arrayResult = array (); 31 32 while ($ iLen = mb_strlen ($ string, $ encoding )) {33 array_push ($ arrayResult, mb_substr ($ string, 0, 1, $ encoding); 34 $ string = mb_substr ($ string, 1, $ iLen, $ encoding ); 35} 36 37 return $ arrayResult; 38} 39 40 // longest common subsequence Chinese version 41 function LCS_cn ($ str1, $ str2, $ encoding = 'utf-8') {42 $ mb_len1 = mb_strlen ($ str1, $ encoding); 43 $ mb_len2 = mb_strlen ($ str2, $ encoding); 44 45 $ mb_str1 = mbStringToArray ($ str1, $ encoding); 46 $ mb_str2 = mbStringToArray ($ str2, $ encoding); 47 48 $ len = $ mb_len1> $ mb_len2? $ Mb_len1: $ mb_len2; 49 50 $ dp = array (); 51 for ($ I = 0; $ I <= $ len; $ I ++) {52 $ dp [$ I] = array (); 53 $ dp [$ I] [0] = 0; 54 $ dp [0] [$ I] = 0; 55} 56 57 for ($ I = 1; $ I <= $ mb_len1; $ I ++) {58 for ($ j = 1; $ j <= $ mb_len2; $ j ++) {59 if ($ mb_str1 [$ I-1] = $ mb_str2 [$ j-1]) {60 $ dp [$ I] [$ j] = $ dp [$ I-1] [$ j-1] + 1; 61} else {62 $ dp [$ I] [$ j] = $ dp [$ I-1] [$ j]> $ dp [$ I] [$ j-1 ]? $ Dp [$ I-1] [$ j]: $ dp [$ I] [$ j-1]; 63} 64} 65} 66 67 return $ dp [$ mb_len1] [$ mb_len2]; 68}




Http://www.bkjia.com/PHPjc/900268.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/900268.htmlTechArticlePHP similar_text (), levenshtein (), lcs () support Chinese character version, PHP native similar_text () function, levenshtein () function is not good support for Chinese characters, I wrote a simila myself...

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.