Home > Article > Backend Development > PHP string splitting and comparison_PHP tutorial
Or use strcmp to judge, but this can tell you whether the two strings are equal, but it cannot tell you where they are different.
My idea is to divide a single string into letters (characters), so that the comparison can accurately know where the difference is.
To separate strings, just use "str_split". For syntax, refer to [2]. Then output the result array. The advantage is that even spaces will be used as elements of the array. My previous example was because the first string contained 2 spaces, while the latter one only had one. But the display you see when outputting is the same.
can also be split according to other delimiters, such as "explode" or "preg_split", refer to [3] and [4]. The former uses a simple split number, and the latter uses regular expressions. "split" has been deprecated.
Another useful function is "str_word_count", which can split a string into an array and clear numbers and punctuation marks. If you want to count the frequency of word occurrences, you can use "array_count_values", refer to [5].
Reference:
【1】http://us2.php.net/manual/en/language.operators.comparison.php
【2】http://us2.php.net/manual/ en/function.str-split.php1
【3】http://us2.php.net/manual/en/function.explode.php
【4】http://us2.php.net/ manual/en/function.str-split.php
【5】http://us2.php.net/manual/en/function.array-count-values.php