Home > Article > Backend Development > Use the function str_contains() in PHP8 to achieve fast string matching
With the development of the Internet, string processing has become a common task in programming. To solve the problem of string matching, PHP8 introduced a new function str_contains(), which can quickly return whether a string contains a specified string. This function will be a very useful tool for developers who need to perform string matching frequently.
1. Basic usage of str_contains() function
str_contains() function is used to determine whether a string contains a specified string. Its syntax is as follows:
bool str_contains( string $haystack, string $needle)
Among them, $haystack represents the original string, and $needle represents the string to be detected. If the original string contains the specified string, the function returns true, otherwise it returns false.
Here are some examples:
Example 1:
$str1 = "Hello World!";
if (str_contains($str1, "World")) {
echo "The string contains the word 'World'";
} else {
echo "The string does not contain the word 'World'";
}
In this example, $haystack is "Hello World!" and $needle is "World". Since $haystack contains $needle, the function will return true and print out the string "The string contains the word 'World'".
Example 2:
$str2 = "Hello World!";
if (str_contains($str2, "Universe")) {
echo "The string contains the word 'Universe'";
} else {
echo "The string does not contain the word 'Universe'";
}
In this example, $haystack is "Hello World!" and $needle is "Universe". Since $haystack does not contain $needle, the function will return false and print out the string "The string does not contain the word 'Universe'".
2. Advantages of str_contains() function
Previously, PHP already had the functions strstr() and strpos(), which are used to find substrings in strings and return their positions. However, the str_contains() function has the following advantages:
if (str_contains($str, "needle")) {
// Code to execute if the string contains "needle"
}
In comparison, use strstr() or strpos( ) function, you need to perform an if conditional judgment on the return value before you can determine whether the match is successful.
3. How to migrate the project to use the str_contains() function
If you want to use the str_contains() function in your project, you need to ensure that the PHP version has been upgraded to 8.0 or above. Once the upgrade is complete, you need to modify the locations in your project where strstr() or strpos() functions are used.
First of all, you need to choose the location that needs to be updated based on the actual situation of the project. If you need to use the function in a large amount of code, you can use the search and replace function to modify the old function call to the new function call.
For example, call the $strstr() function:
if (strstr($haystack, $needle)) {
// ...
}
Modify Call the $str_contains() function:
if (str_contains($haystack, $needle)) {
// ...
}
In this way, your project can start using str_contains() function.
4. Conclusion
The str_contains() function is one of the string matching functions introduced in PHP8. It has stronger performance and is more concise and clear to use. If you are still using the strstr() or strpos() function, it is recommended to actively migrate to the str_contains() function if necessary.
The above is the detailed content of Use the function str_contains() in PHP8 to achieve fast string matching. For more information, please follow other related articles on the PHP Chinese website!