Home >Backend Development >PHP Problem >php determines whether it contains characters
When writing a program, you often need to process strings. The most basic thing is to search for strings. To detect whether a string contains a specified string in PHP, you can use regular expressions. If you don’t understand regular expressions, there are several functions that can For your convenience.
strpos() function determines whether a string contains a certain string
strstr (Recommended learning: PHP video tutorial)
strstr() function searches for the first occurrence of a string in another string.
This function returns the rest of the string (from the matching point). Returns false if the searched string is not found.
The code is as follows:
<?php /*如手册上的举例*/ $email = 'user@example.com'; $domain = strstr($email, '@'); echo $domain; // prints @example.com?>
stristr
stristr() function finds the first string in another string The position where it appears.
If successful, return the remainder of the string (from the point of the match). If the string is not found, returns false.
It is used exactly the same as strstr. The only difference is that strstr is not case-sensitive.
strpos
The strpos function returns a boolean value. Needless to say, FALSE and TRUE. Use "===" to judge. strpos is faster than the above two functions in terms of execution speed. In addition, strpos has a parameter to specify the judgment position, but the default is empty. It means to judge the entire string. .The disadvantage is that the support for Chinese is not good.
Example
if(strpos('www.jb51.net','jb51') !== false){ echo '包含jb51'; }else{ echo '不包含jb51'; }
The above is the detailed content of php determines whether it contains characters. For more information, please follow other related articles on the PHP Chinese website!