Home > Backend Development > PHP Problem > php determines whether it contains characters

php determines whether it contains characters

(*-*)浩
Release: 2023-02-27 11:56:01
Original
6571 people have browsed it

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.

php determines whether it contains characters

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 = &#39;user@example.com&#39;;
 $domain = strstr($email, &#39;@&#39;);
 echo $domain;
 // prints @example.com?>
Copy after login

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(&#39;www.jb51.net&#39;,&#39;jb51&#39;) !== false){ 
 echo &#39;包含jb51&#39;; 
}else{
 echo &#39;不包含jb51&#39;; 
}
Copy after login

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!

Related labels:
php
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