


PHP8.1's new str_contains function: quickly determine whether a substring exists
PHP8.1’s new str_contains function: quickly determine whether a substring exists
In the latest PHP8.1 version, a very convenient function str_contains has been added, its function is to Quickly determine whether a string contains another substring. Compared with the previous strpos function, the str_contains function is more concise and easy to use, which can greatly improve development efficiency. This article will introduce you to the use of str_contains function and provide some code examples.
In previous versions, to determine whether a string contains another substring, the strpos function was often used. For example, if we want to determine whether the string $str contains the substring "world", we can achieve this through the following code:
if(strpos($str, 'world') !== false){ echo "包含子字符串"; }else{ echo "不包含子字符串"; }
The above code first uses the strpos function to find the substring in the string. position, if the return value is not false, it means that the substring exists in the string, and we can output "contains substring". Otherwise, output "does not contain substring".
However, there are some problems with using the strpos function for judgment. First, the return value of the strpos function may be 0, indicating that the substring appears at the beginning of the string, which can be confused with the return value when the substring does not exist. Secondly, the strpos function requires writing more code to implement judgment, which does not seem intuitive enough. Therefore, in the new version of PHP8.1, the str_contains function was introduced to solve these problems.
The use of the str_contains function is very simple. You only need to pass in two parameters. The first parameter is the string to be searched, and the second parameter is the substring to be searched. The function returns a bool value indicating whether the substring exists in the string.
The following is a sample code using the str_contains function:
$str = "Hello, world!"; if(str_contains($str, 'world')){ echo "包含子字符串"; }else{ echo "不包含子字符串"; }
In the above code, if the string $str contains the substring "world", then "contains substring" is output; otherwise , output "does not contain substring". It can be seen that compared to using the strpos function, using the str_contains function is more intuitive and concise.
In addition to being used to determine whether a single string contains a substring, the str_contains function can also be used for each element in an array or string. The following is a sample code:
$fruits = ['apple', 'banana', 'orange']; if(str_contains($fruits, 'banana')){ echo "数组中包含子字符串"; }else{ echo "数组中不包含子字符串"; }
In the above code, determine whether the array $fruits contains the substring "banana". If it does, output "the array contains the substring"; otherwise, output "array" does not contain substring".
In summary, the new str_contains function in PHP8.1 provides us with a simple and intuitive way to determine whether a string contains substrings. Its use is very simple, you only need to pass in two parameters and determine the return value. Whether you are testing a single string or each element in an array or string, the str_contains function provides a convenient solution. When you start using PHP8.1 version, remember to try this convenient function!
The above is the detailed content of PHP8.1's new str_contains function: quickly determine whether a substring exists. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

How to use the LOCATE function in MySQL to find the position of a substring in a string. In MySQL, there are many functions that can be used to process strings. Among them, the LOCATE function is a very useful function that can be used to find the position of a substring in a string. The syntax of the LOCATE function is as follows: LOCATE(substring,string,[position]) where substring is the substring to be found and string is the substring to be found.

This function is similar to the strtok() function. The only key difference is _r, which is called a reentrant function. Reentrant functions are functions that can be interrupted during execution. This type of function can be used to resume execution. Therefore, reentrant functions are thread-safe, which means they can be safely interrupted by threads without causing any damage. The strtok_r() function has an extra parameter called context. This way the function can be restored in the correct location. The syntax of the strtok_r() function is as follows: #include<string.h>char*strtok_r(char*string,constchar*limiter,char**

Over time, programming languages continue to upgrade in terms of functionality and performance. PHP is a common server-side language. Starting from PHP7, the language has come with many differences, including enhanced type inference, null coalescing operator, better exception handling and high speed. PHP8.0 is the latest version of PHP, which brings some new functions and language features, the most notable of which is the str_contains function. Let us learn more about str_cont in PHP8.0

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

PHP8.1’s new str_contains function: quickly determine whether a substring exists. In the latest PHP8.1 version, a very convenient function str_contains has been added. Its function is to quickly determine whether a string contains another string. substring. Compared with the previous strpos function, the str_contains function is more concise and easy to use, which can greatly improve development efficiency. This article will introduce to you how to use the str_contains function and

Regular expressions are a powerful text processing tool that can be used to match strings in specific patterns. In PHP, regular expressions are commonly used in string processing, form validation, search and replacement, etc. This article will introduce how to use PHP's regular expressions to extract specific characters from a string to the substring at the end. First, let's look at an example. Suppose we have a string $str that contains multiple URLs starting with "http://" and we want to extract these URLs and store them in a

In this tutorial, we need to solve a palindrome substring query for a given string. Solving palindrome substring queries is much more complex than solving regular queries in C++. It requires more complex code and logic. In this tutorial, we have provided string str and Q substring [L...R] queries, each of which has two values L and R. Our goal is to write a program that solves a query to determine if substring[L...R] is a palindrome. We have to determine whether the substring formed in the range L to R is a palindrome to solve each query. For example-Let'sinput"abbbabaaaba"asourinputstring.Thequer
