PHP search string common function introduction

  • 2020-05-17 04:59:12
  • OfStack

1. strstr - the first occurrence of a search string

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
Note 1: $haystack is the party string and $needle is the string to be found. This function is case sensitive.
Note 2: the return value is from needle to the end.
Note 3: about $needle, if it is not a string, it is used as an integer as the ordinal number of the character.
Note 4: if before_needle is true, it returns the previous thing.
 
<?php 
$email = 'yuxiaoxiao@example.com'; 
$domain = strstr($email, '@'); 
echo $domain; //  print  @example.com 
$user = strstr($email, '@', true); //  from  PHP 5.3.0  since  
echo $user; //  print  yuxiaoxiao 
?> 


2. stristr strstr case-insensitive version
3. strpos - looks for the first occurrence of a string

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
Note 1: the optional offset parameter can be used to specify which character in haystack to start with. The numeric position returned is relative to the starting position of haystack.
4. substr - returns a substring of a string

string substr ( string $string , int $start [, int $length ] )
$rest = substr (" abcdef ", 1); / / return "f"

Note 1: if start is non-negative, the returned string will start at the start position of string and start at 0. For example, in the string "abcdef," the character at position 0 is "a," the string at position 2 is "c," and so on.

Note 2: if start is a negative number, the returned string begins at the end of string and begins at start.

Note 3: if the length of string is less than or equal to start, FALSE is returned.

length

Note 4: if a positive length is provided, the returned string will contain up to length characters starting at start (depending on the length of string).

Note 5: if length with a negative number is provided, many characters at the end of string will be omitted (if start is negative, start at the end of the string). If start is not in the text, an empty string is returned.

Note 6: if an length with a value of 0, FALSE or NULL is provided, an empty string is returned.

Note 7: if length is not provided, the returned substring begins at the start position and ends at the end of the string.
 
<?php 
$rest = substr("abcdef", 0, -1); //  return  "abcde" 
$rest = substr("abcdef", 2, -1); //  return  "cde" 
$rest = substr("abcdef", 4, -4); //  return  "" 
$rest = substr("abcdef", -3, -1); //  return  "de" 
?> 

5. strrchr - looks for the last occurrence of a specified character in a string

string strrchr ( string $haystack , mixed $needle )

This function returns the first part of the haystack string, which begins at the last occurrence of needle until the end of haystack.

6. strripos - calculates the last occurrence of the specified string in the target string (case insensitive)
7. stripos - find the location where the string first appears (regardless of size)
8. strrpos - calculates the last occurrence of the specified string in the target string

Related articles: