PHP8.0 new function analysis: str_contains()

WBOY
Release: 2023-05-16 08:34:01
Original
3075 people have browsed it

PHP 8.0 is the latest version of the PHP programming language, which brings many new features and enhancements, one of the new functions is str_contains(). This function is used to determine whether a string contains another string.

In this article, we will explore the usage and examples of the str_contains() function and how it works with PHP’s other string functions.

1. Definition of str_contains() function

The str_contains() function can be used in PHP to determine whether a string contains another string. It can take two strings as input parameters and return a boolean value that returns true if the second string is contained in the first string and false otherwise.

The following is the basic syntax of the str_contains() function:

bool str_contains (string $haystack, string $needle)

Among them, $haystack is the string to be searched, $needle is the substring to find. Returns true if $needle is in $haystack, false otherwise.

It is worth mentioning that this function is only available in PHP 8.0 and above.

2. Examples of str_contains() function

Here, we will use some examples to illustrate the usage scenarios of str_contains() function. First, let's look at a basic example, as follows:

$str1 = "Hello, World!";
$str2 = "World";
if(str_contains($str1, $ str2)){
echo "The string '$str1' contains '$str2'";
}else{
echo "The string '$str1' does not contain '$str2'";
}

The example passes the $str1 and $str2 variables to the str_contains() function and uses a conditional statement to determine whether the first string contains the second string. If included, print matching results, otherwise print non-matching results.

Now suppose we have an array of strings and we want to find elements that contain a specified substring. We can use the str_contains() function to achieve this:

$array = array("apple", "banana", "kiwi", "orange");
$search = "ki";
foreach($array as $fruit){
if(str_contains($fruit, $search)){

 echo "Found matching element: $fruit <br>";
Copy after login

}
}

In this example, we are in a Find elements containing "ki" in a string array. We use a foreach loop to iterate through all elements in the array and use the str_contains() function to search on each string. If an element matching the criteria is found, we print it.

3. Use with other string functions

The str_contains() function can be used with many other string functions, including strpos() and strstr(). Let's use an example to show how they work together:

$str = "The quick brown fox jumps over the lazy dog.";
$search = "fox";
if(strpos ($str, $search) !== false){
echo "The string '$str' contains '$search'.
";
}
if(strstr($str, $ search)){
echo "The string '$str' contains '$search'.
";
}
if(str_contains($str, $search)){
echo " The string '$str' contains '$search'.
";
}

In this example, we use strpos(), strstr() and str_contains( ) function to find the "fox" substring. Each function performs the same task, but uses a different way to determine whether a string contains a substring.

4. Conclusion

The str_contains() function is a powerful new function in PHP 8.0, which can be used to find whether a string contains another string in PHP. Whether in basic applications or highly dynamic applications, it helps us accomplish string search tasks easily.

All the examples mentioned in this article can be used in practical applications, let's start enjoying this new feature!

The above is the detailed content of PHP8.0 new function analysis: str_contains(). For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!