PHP String Matching Algorithm Sunday Algorithm

Source: Internet
Author: User

Search Text text = "My Testing Algorithm in test"

Pattern = "test"

The key aspect of the Sunday algorithm is

1. set a shift [] matching shift ing. The shift [] ing relationship must start from left to right. For example, pattern = "test". Note that there are 2 t maps, then the established displacement ing is shift [] = array ([T] => 1 [e] => 3 [s] => 2), and if it is not left to right, is the ing from right to left, it will become shift [] = array ([T] => 4 [e] => 3 [s] => 2 ), in this way, the matching results will not be able to get the correct results.

2. Determine the displacement Length Based on the next character of the current comparison string, as shown in figure

In the first comparison, 1, the first character "M" is different from "T", so we can find the character in text that is 1 bit longer than patter, which is "E ", then look for the ing table, E => 3. Next, move pattern three places backward, and the position in Figure 2 is reached. then compare it from "T" and find that the matching continues until the next step, look at the character in text that is 1 longer than pattern, which is "I". If no "I" is found in the ing table, move pattern backward to the pattern_size bit, and then to figure 3, repeat the previous process until the character whose text_size-patter_size is the coordinate is compared.

 

The following code is used:

1 <? PHP 2 # string matching, Sunday algorithm 3 4 function Sunday ($ patt, $ text) {5 $ patt_size = strlen ($ patt); 6 $ text_size = strlen ($ text ); 7 8 # initialize the string displacement ing relationship 9 # note that the ing relationship table must be created from left to right, because Patten may have the same character 10 # For the displacement length of repeated characters, we can only make the displacement length of the last repeated character overwrite the displacement length before 11 # For example, pattern = "testing". Note that there are 2 T, the established displacement ing is shift [] = array ([T] => 4 [e] => 6 [s] => 5 [I] => 3 [N] => 2 [g] => 1) 12 # If the ing is established from right to left instead of from left to right, it will become shift [] = array ([T] => 7 [E] => 6 [s] => 5 [I] => 3 [N] => 2 [g] => 1 ), in this way, the matching result will fail to get the correct result 13 for ($ I = 0; $ I <$ patt_size; $ I ++) {14 $ shift [$ patt [$ I] = $ patt_size-$ I; 15} 16 17 $ I = 0; 18 $ Limit = $ text_size-$ patt_size; # The coordinates of the last character to be matched 19 while ($ I <= $ limit) {20 $ match_size = 0; # Number of matched characters 21 # match string 22 from I while ($ text [$ I + $ match_size] ==$ patt [$ match_size]) {23 $ match_size ++; 24 if ($ match_size = $ patt_size) {25 echo "m Atch index: {$ I} <br> "; 26 break; 27} 28} 29 30 $ shift_index = $ I + $ patt_size; #31 if ($ shift_index <$ text_size & isset ($ shift [$ text [$ shift_index]) {32 $ I + = $ shift [$ text [$ shift_index]; 33} else {34 # If the ing table does not contain the displacement of this character, direct backward movement of patt_size units 35 $ I + = $ patt_size; 36} 37} 38} 39 40 $ text = "My Testing Algorithm test"; 41 $ patt = "test "; 42 43 Sunday ($ patt, $ text); 44?>

 

 

Match index: 3
Match index: 21

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.