Home  >  Article  >  Backend Development  >  How to query items containing specific characters in PHP (4 ways)

How to query items containing specific characters in PHP (4 ways)

PHPz
PHPzOriginal
2023-04-10 09:44:372666browse

PHP is a popular server-side scripting language. In web development, it is often necessary to query for items containing a specific string. This article will introduce how to query methods containing strings in PHP.

  1. strstr() function

strstr() function is used to find another string in a string and return the substring from that string to the end of the string. string. If not found, returns false. The syntax of this function is as follows:

strstr(string $haystack , mixed $needle [, bool $before_needle = false ] ) : string|false

Among them, $haystack represents the string to be searched, $needle represents the string to be found, and $before_needle represents whether to truncate the substring to $needle.

For example, to search whether the string "abcdef" contains "cd", the code is as follows:

if (strstr("abcdef", "cd")) {
    echo "字符串中包含'cd'";
} else {
    echo "字符串中不包含'cd'";
}

This code will output "the string contains 'cd'". The sample code illustrates how to use the strstr() function to find a string in PHP.

  1. strpos() function

The strpos() function is used to find the first occurrence of a substring in a string and return it as an integer. If not found, returns false. The syntax of this function is as follows:

strpos(string $haystack , mixed $needle [, int $offset = 0 ] ) : int|false

Among them, $haystack represents the string to be searched, $needle represents the string to be found, and $offset represents the position to start the search.

For example, to search whether the string "abcdef" contains "cd", the code is as follows:

if (strpos("abcdef", "cd") !== false) {
    echo "字符串中包含'cd',位置为:" . strpos("abcdef", "cd");
} else {
    echo "字符串中不包含'cd'";
}

This code will output "The string contains 'cd', position: 2" . The sample code illustrates how to use the strpos() function to find a string in PHP.

  1. preg_match() function

The preg_match() function is used to perform a regular expression match in a string and return whether a match is found. If not found, returns 0; otherwise returns 1. The syntax of this function is as follows:

preg_match(string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) : int|false

Among them, $pattern is a regular expression, $subject represents the string to be matched, $matches represents saving the matching results (optional), and $flags represents the matching flag ( Optional), $offset indicates the starting position of matching (optional).

For example, to search whether the string "abcdef" contains "c or d", the code is as follows:

if (preg_match("/[cd]/", "abcdef")) {
    echo "字符串中包含'c'或'd'";
} else {
    echo "字符串中不包含'c'或'd'";
}

This code will output "The string contains 'c' or 'd' ". The sample code illustrates how to use the preg_match() function to find a string in PHP.

  1. stripos() function

stripos() function is used to find the first occurrence of a substring in a string and return it as an integer. Unlike the strpos() function, it is not case sensitive. If not found, returns false. The syntax of this function is as follows:

stripos(string $haystack , mixed $needle [, int $offset = 0 ] ) : int|false

The remaining parameters are the same as the strpos() function.

For example, to search whether the string "abcdef" contains "CD", the code is as follows:

if (stripos("abcdef", "CD") !== false) {
    echo "字符串中包含'CD',位置为:" . stripos("abcdef", "CD");
} else {
    echo "字符串中不包含'CD'";
}

This code will output "The string contains 'CD', position: 2" . The sample code illustrates how to use the stripos() function to find a string in PHP.

Summary

As a popular server-side scripting language, PHP's built-in functions provide many convenient methods to manipulate strings. Whether it's the strstr() function, strpos() function, preg_match() function, or stripos() function, there are different ways to find items that contain strings. Developers can choose appropriate functions to process string queries based on their needs.

The above is the detailed content of How to query items containing specific characters in PHP (4 ways). 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