Several methods of php to determine whether a string contains a specified string

When writing programs, you often have to deal with strings. The most basic thing is to search for strings. You can use regular expressions to detect whether a string contains a specified string in PHP. If you don’t know about regular expressions, there are several functions that can provide you with convenient.

  1. strstr

The strstr() function searches for the first occurrence of a string within another string.
The function returns the rest of the string (from the matching point). Returns false if the searched string is not found.

code show as below:

<?php 
 /*Example in the manual*/ 
 $email = '[email protected]' ;
  $domain = strstr( $email , '@' );
  echo  $domain ;
  // prints @example.com 
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. stristr

The stristr() function finds the first occurrence of a string within another string.
If successful, return the rest of the string (from the matching point). Returns false if the string is not found.
It is used exactly the same way as strstr. The only difference is that strstr is not case sensitive.

  1. strpos

The strpos function returns a boolean value. FALSE and TRUE needless to say. Use "===" to judge. strpos is faster than the above two functions in execution speed. In addition, strpos has a parameter to specify the position of the judgment, but it is empty by default. It means to judge the whole string. The disadvantage is that the support for Chinese is not good.

Example 1

<php
 if (strpos( 'www.jb51.net' , 'jb51' ) !== false ){ 
  echo  'includes jb51' ;
} else {
  echo  'does not contain jb51' ;
}
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Example 2

$str = 'abc' ;
 $needle = 'a' ;
 $pos = strpos( $str , $needle ); // Return to the position where the changed string was found for the first time, here it returns 1 , if not found, return false
  • 1
  • 2
  • 3
  1. explode

Use explode to judge the inclusion code of the PHP judgment string as follows:

function  checkstr ( $str ) { 
 $needle = 'a' ; //Determine whether the character a contains a 
 $tmparray = explode( $needle , $str );
  if (count( $tmparray )> 1 ){
   return  true ;
 } else{
  return false;
 }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

<?php
/*
$str1 = "<p>This is a dll dedicated to winrar and then download a good dll file, QlogWin32.dll</p>" ;
 if ( substr ( $str1 ,- 8 )== ".dll</p> " ){
echo substr($str1,0,-4);
}
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

6. Substr_count counts the number of occurrences of "substring" in "original string"

The substr_count() function is the number of times a small string appears in a large string:
$number = substr_count(big_string, small_string);
Just today I need a function to find a string, to determine whether the string big_string contains a string small_string, returns true or fasle;

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

function check_str($str, $substr)
{
 $nums=substr_count($str,$substr);
  if ($nums>=1)
  {
   return true;
  }
  else
  {
   return false;
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Super easy!

Specifically, you can look up related functions for advanced applications.

Related: Several methods of php to determine whether a string contains a specified string