Home > Backend Development > PHP Tutorial > php detect if string contains string

php detect if string contains string

王林
Release: 2023-04-07 13:42:02
Original
11990 people have browsed it

php detect if string contains string

1. strstr

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

2. stristr

stristr() function finds a string in another string The position where it first appears. If successful, the remainder of the string (from the point of the match) is returned. 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.

3. strpos

The strpos function returns a boolean value. FALSE and TRUE are not used Say more. 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 it is Chinese support is not good.

Example 1:

if(strpos(&#39;www.jb51.net&#39;,&#39;jb51&#39;) !== false){ 
 echo &#39;包含jb51&#39;; 
}else{
 echo &#39;不包含jb51&#39;; 
}
Copy after login

Example 2:

$str= &#39;abc&#39;;
$needle= &#39;a&#39;;
$pos = strpos($str, $needle); // 返回第一次找到改字符串的位置,这里返回为1,若查不到则返回Fals
Copy after login

4. explode

Use explode to judge. The PHP judgment string contains the following code:

function checkstr($str){
 $needle =&#39;a&#39;;//判断是否包含a这个字符
 $tmparray = explode($needle,$str);
 if(count($tmparray)>1){
 return true;
 } else{
 return false;
 }
}
Copy after login

5. substr. For example, we need to judge whether the last character is a specified character

<?php
/*
$str1="<p>这是个winrar专用的dll然后下哦啊不错的dll文件,QlogWin32.dll</p>";
if(substr($str1,-8)==".dll</p>"){
echo substr($str1,0,-4);
}
Copy after login

6. substr_count counts the number of times "substring" appears in the "original string"

substr_count() function is originally a small string The number of occurrences in a large string:

$number = substr_count(big_string, small_string);
Copy after login

Just today I need a function to find a string. To determine whether the string big_string contains the string small_string, return true or fasle;

After checking the manual for a long time, I couldn't find a ready-made function, so I thought that I could use the substr_count function to implement the code as follows:

function check_str($str, $substr)
{
 $nums=substr_count($str,$substr);
 if ($nums>=1)
 {
  return true;
 }
 else
 {
  return false;
 }
}
Copy after login

Recommended tutorial: PHP video tutorial

The above is the detailed content of php detect if string contains string. For more information, please follow other related articles on the PHP Chinese website!

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