Introduction to php string splitting and comparison

怪我咯
Release: 2023-03-13 07:04:01
Original
2135 people have browsed it

Compare two strings to see if they are equal. The most common way is to use "===" to judge. As for the difference between it and "==", simply put, the former emphasizes "Identical" The type also requires the same; the latter requires "Equal", and the values ​​​​are the same.

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, 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.
You can also 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 expression. "split" has been deprecated.
Another very useful function is "str_word_count", which can split a string into an array and clear numbers and punctuation marks at the same time. If you want to count the frequency of words, you can use "array_count_values",

The above is the detailed content of Introduction to php string splitting and comparison. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template