Whether the php string contains multiple methods for specifying the string

  • 2021-09-20 19:38:30
  • OfStack

When you write a program, you often have to deal with strings. The most basic thing is to find strings. In php, you can use regularity to detect whether a string contains a specified string. If you don't understand regularity, there are several functions that can provide convenience for you.

The strpos () function determines whether the string contains a string https://www. ofstack. com/article/154741. htm

1. strstr

The strstr () function searches for the first occurrence of one string in another.
This function returns the rest of the string (from the matching point). If the searched string is not found, false is returned.

The code is as follows:


<?php
 /* Such as the example in the manual */
 $email = 'user@example.com';
 $domain = strstr($email, '@');
 echo $domain;
 // prints @example.com
?>

2. stristr

The stristr () function looks for the first occurrence of a string in another string.
If successful, the rest of the string (from the matching point) is returned. If the string is not found, false is returned.

It and strstr use exactly the same way. The only difference is that stristr is not case sensitive.

3. strpos

strpos function returns 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 judgment position, but the default is empty. It means to judge the whole string. The disadvantage is that it does not support Chinese well.

Example 1


if(strpos('www.ofstack.com','jb51') !== false){ 
 echo ' Include jb51'; 
}else{
 echo ' Does not contain jb51'; 
}

Example 2


$str= 'abc';
$needle= 'a';
$pos = strpos($str, $needle); //  Return to the 1 Find the position of the changed string again, where it returns as 1 If it cannot be found, it will return False

4. explode

Judgment with explode the code for the PHP judgment string is as follows:


function checkstr($str){
 $needle ='a';// Determine whether to include a This character 
 $tmparray = explode($needle,$str);
 if(count($tmparray)>1){
 return true;
 } else{
 return false;
 }
}

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


<?php
/*
$str1="<p> This is a winrar Dedicated dll And then go oh oh good dll Documents, QlogWin32.dll</p>";
if(substr($str1,-8)==".dll</p>"){
echo substr($str1,0,-4);
}

6. substr_count Count the number of times "substring" appears 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, we need a function to find a string. To judge whether the string big_string contains the string small_string, return true or fasle;

After checking the manual for half a day, I didn't find a ready-made function, so I thought that I could use substr_count function to realize the code as follows:


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

Super simple!

Specific everyone can find 1 related functions for advanced application.


Related articles: