Home > Article > Backend Development > How to query whether a string exists in php
The PHP programming language is a very powerful and popular back-end programming language that is widely used in the development and maintenance of web applications. During the development process, we often need to query and process data, especially the processing of string data, such as finding whether a certain substring exists in a string. In this article, we will introduce how to use PHP for string query, aiming to help beginners quickly solve this common problem.
1. Use the strpos() function to query strings
PHP provides many built-in functions to process string data, the most commonly used of which is the strpos() function. This function can be used to find whether a string contains a substring and return the position of the substring in the parent string.
The following is the basic syntax for using the strpos() function:
strpos($string, $target)
Among them, $string represents the string to be found, and $target represents the target string to be searched. If the returned result is false, it means that the search failed, otherwise the function returns the position of the target string in the parent string.
Take the following code as an example:
$str = "Hello, World!"; $needle = "World"; if (strpos($str, $needle) !== false) { echo "Found"; } else { echo "Not found"; }
This code will search for the substring $needle
in the string $str
, if found Then output Found
, otherwise output Not found
. In this case, the output is Found
because $needle
does exist in the string $str
.
2. Use the preg_match() function to query strings
In addition to using the strpos() function, PHP also provides the preg_match() function for regular expression matching. Regular expression is a tool that can accurately match, replace and extract strings, and has powerful text processing capabilities.
The following is the basic syntax for using the preg_match() function:
preg_match($pattern, $string, $matches)
Among them, $pattern represents the pattern of the regular expression, $string represents the string that needs to be matched, and $matches represents the matching results. output variable. The preg_match() function will return 1 if the match is successful, otherwise it will return 0.
Take the following example:
$str = "I have 3 apples."; $pattern = "/\d+/"; if (preg_match($pattern, $str, $matches)) { echo "Found number: " . $matches[0]; } else { echo "Not found"; }
This code will use regular expressions to find numbers in the string $str
and output the result. In this example, the output is Found number: 3
.
3. Use the strstr() function to query strings
In addition to using the strpos() function, PHP also provides other functions to find substrings, such as the strstr() function. This function finds the first occurrence of a substring and returns that substring and all characters that follow it.
The following is the basic syntax for using the strstr() function:
strstr($string, $needle, $before_needle = false)
Among them, $string represents the string to be found, $needle represents the target string to be searched, and $before_needle represents whether to return The string preceding the target string. If this parameter is true, the function returns the string preceding the target string.
Take the following code as an example:
$str = "Hello, World!"; $needle = "Wor"; if (strstr($str, $needle)) { echo "Found"; } else { echo "Not found"; }
This code will search for the substring $needle
in the string $str
, if found Then output Found
, otherwise output Not found
. In this case, the output is Found
because $needle
does exist in the string $str
.
4. Use the substr_count() function to query a string
PHP also provides the substr_count() function to count the number of times a string appears in another string. The function is very simple and requires only two string parameters.
The following is the basic syntax for using the substr_count() function:
substr_count($string, $needle)
Among them, $string represents the string to be found, and $needle represents the target string to be searched. The function returns the number of occurrences of the target substring in the parent string.
Take the following code as an example:
$str = "Hello, World!"; $needle = "l"; $count = substr_count($str, $needle); echo "Found " . $count . " times";
This code will find the substring $needle
in the string $str
and output it The number of occurrences. In this example, the output is Found 3 times
.
5. Use the stristr() function to query strings (case-insensitive)
Finally, there is a special function stristr(), which is very similar to the strstr() function, but not case sensitive. This means that regardless of the case of the target string, the function will be able to find it correctly.
The following is the basic syntax for using the stristr() function:
stristr($string, $needle)
Among them, $string represents the string to be found, and $needle represents the target string to be searched. The function returns the target substring and all subsequent characters. If the target string does not exist, returns false.
Take the following code as an example:
$str = "Hello, World!"; $needle = "worL"; if (stristr($str, $needle)) { echo "Found"; } else { echo "Not found"; }
This code will find the case-insensitive substring $needle# in the string
$str ##, if found, output
Found, otherwise output
Not found. In this case, the output is
Found because
$needle does exist in the string
$str.
The above is the detailed content of How to query whether a string exists in php. For more information, please follow other related articles on the PHP Chinese website!