Home  >  Article  >  Backend Development  >  How to determine whether an array exists in a string in php

How to determine whether an array exists in a string in php

PHPz
PHPzOriginal
2023-04-18 10:20:191314browse

In PHP, determining whether an array exists in a string is a common problem. PHP provides a variety of methods to achieve this function, and this article will introduce some of the more common methods.

Method 1: Use the in_array function

The in_array function is a very commonly used function in PHP, used to determine whether a value is in an array. We can convert the string into an array, and then use the in_array function to determine whether the array we need to find exists in the array. The sample code is as follows:

$str = 'apple,banana,pear';
$arr = explode(',', $str);
if (in_array('banana', $arr)) {
    echo '存在';
} else {
    echo '不存在';
}

Code description:

First, we define a string variable $str, which contains three fruit names, separated by commas. Then split the $str string into the array $arr through the explode function. Finally, use the in_array function to determine whether the element 'banana' exists in the array $arr. If it exists, output 'exists', otherwise output 'does not exist'.

Method 2: Use strpos and explode functions

Another method is to use strpos and explode functions. The strpos function is used to search for the position of a certain substring in a string. If it is found, it returns an integer value, otherwise it returns false. We can split the string $str with commas as delimiters, and then use the strpos function to determine whether the element to be found is in the returned array. The sample code is as follows:

$str = 'apple,banana,pear';
if (strpos($str, 'banana') !== false) {
    echo '存在';
} else {
    echo '不存在';
}

Code description:

First, we define a string variable $str, which contains three fruit names, separated by commas. Then, use the strpos function to find the position of 'banana' in $str. If it is found, it will return the position, otherwise it will return false. Finally, "!==" must be used to judge through a conditional expression, because the strpos function may return 0, which is false, but it is meaningful, and if you use "!=", it will become true with false.

Method 3: Use regular expressions

If you are familiar with regular expressions and have certain performance requirements, then use regular expressions to determine whether an array exists in a string is a good choice. We can convert the array we need to find into a regular expression, and then use the preg_match function to match. If the match is successful, it will return true, otherwise it will return false. The sample code is as follows:

$str = 'apple,banana,pear';
if (preg_match('/banana/', $str)) {
    echo '存在';
} else {
    echo '不存在';
}

Code description:

First define the string variable $str, and then use the preg_match function to determine whether the 'banana' string exists in the variable $str. If it exists, output 'exists', otherwise output 'does not exist'.

Summary

This article introduces how to determine whether an array exists in a string in PHP, including three common methods. The specific method to choose depends on the actual situation. Generally speaking, in_array, strpos, and explode functions have better performance and readability, while regular expressions are more suitable for use in advanced programs.

The above is the detailed content of How to determine whether an array exists in a string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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