Example analysis of php string lookup functions strstr of and strpos of

  • 2021-12-12 08:08:01
  • OfStack

In this paper, the common string lookup functions strstr () and strpos () are described by examples. Share it for your reference, as follows:

1 sentence is judged by strpos === Or !== In order to achieve the desired effect, the performance is better than strstr, just to determine whether a string is included with this.

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

1, $haystack the string to be looked up, $needle the content to be looked up
2. If found, return part 1 of the string, if not found, return FALSE
3. This function is case-sensitive. If you want to be case-insensitive, use stristr()
4. If you just want to determine whether needle exists in haystack, please use a faster and less memory consuming one strpos() Function


<?php
 $email = 'name@example.com';
 $domain = strstr($email,'@');
 $name = strstr($email,'@',TRUE);
 $no_con = strstr($email,'99');
 echo $domain; // Output  @example.com
 echo $name;  // Output name  From  PHP 5.3.0  Rise 
 var_dump($no_con); // If not found, a Boolean value is returned  FALSE
?>

Run results:

@example.com
name
bool(false)

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

1, $haystack the string to be looked up, $needle the content to be looked up
2. Returns the number position where needle first appears in haystack
3. This function is case-sensitive. If you want to be case-insensitive, use stripos()
4. Return value. If found, needle exists at the beginning of haystack string (note that the string position starts from 0 instead of 1). If not, FALSE is returned, but it may also return a non-Boolean value equivalent to FALSE


<?php
 $mystring = 'abc' ;
 $findme = 'a' ;
 $pos = strpos($mystring,$findme);
 echo $pos; // Output 0, Both at present a Location of 
?>

Run results:

0

Here are two similar functions, briefly introduced here, just remember that there is this function, and simply look at the manual when you use it.

1. strrpos() Calculates the last occurrence of the specified string in the target string

Example 1 uses = = =


<?php
$mystring = 'abc';
$findme  = 'a';
$pos = strpos($mystring, $findme);

//  Note that what is used here is  === . Simple  ==  Can't work as we expect, 
//  Because  'a'  Is the first  0  In the position (the first 1 Characters. 
if ($pos === false) {
  echo "The string '$findme' was not found in the string '$mystring'";
} else {
  echo "The string '$findme' was found in the string '$mystring'";
  echo " and exists at position $pos";
}
?>

Example 2 uses! = =


<?php
$mystring = 'abc';
$findme  = 'a';
$pos = strpos($mystring, $findme);

//  Use  !==  Operator. Use  !=  Can't work as we expect, 
//  Because  'a'  The location of is  0 . Statement  (0 != false)  The result is that  false . 
if ($pos !== false) {
   echo "The string '$findme' was found in the string '$mystring'";
     echo " and exists at position $pos";
} else {
   echo "The string '$findme' was not found in the string '$mystring'";
}
?>

Example 3 Using Position Offsets


<?php
//  Ignore the characters before the position offset for lookup 
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7,  No  0
?>

Notes
Note: This function is safe for binary objects.

2. strripos() Calculates the last occurrence of the specified string in the target string (case-insensitive)

Summary: Note that these functions will return FALSE if they are not found, so when judging whether the two sides are equal (if), pay attention to the types of the two sides. The above functions are commonly used string finding functions in PHP. If more powerful functions are needed, such as matching and verifying mailboxes and mobile phone numbers, they need to be completed by regular expressions.

For more readers interested in PHP related contents, please check the topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"

I hope this article is helpful to everyone's PHP programming.


Related articles: