Usage of character lookup functions strpos strrchr and strpbrk in php

  • 2021-08-03 09:34:06
  • OfStack

In this paper, the usage of character lookup functions strpos, strrchr and strpbrk in php is described by examples. Share it for your reference. The details are as follows:

The strpos () function returns the first occurrence of a string in another string, or false if the string is not found.

Syntax: strpos (string, find, start), code as follows:

$str="hello world";          // Definition string 1 
$result=strpos($str,"ll");         // Perform the search for the earliest occurrence location
echo $result;           // Output results, 2

② The strrchr () function finds the last occurrence position of a string in another string, and returns all characters from this position to the end of the string. If it fails, it returns false otherwise.

Syntax: strrchr (string, char), code as follows:

$str="hello world";          // Definition string 1 
$result=strrchr($str,"o");         // Perform a search for the last occurrence location
echo $result;

③ The strpbrk () function searches for any one of the specified characters in the string. The function returns the remaining part from the first occurrence of the specified character, and if it is not found, it returns false.

Syntax: strpbrk (string, charlist), code as follows:

$str="hello world";    // Definition string 1 
$result=strpbrk($str,"oe");       // Perform a lookup operation
echo $result;     // Output results, hello world

Tips and comments.

Note: This function is case sensitive.

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


Related articles: