Home  >  Article  >  Backend Development  >  PHP string search function (strrpos and strchr)

PHP string search function (strrpos and strchr)

WBOY
WBOYOriginal
2016-07-25 08:54:011348browse
  1. $str = '1234567890';
  2. $needle = '234';
  3. $result = strchr($str, $needle);
  4. var_dump($result);
Copy code

Result :string '234567890'(length=9)

strrpos finds the last occurrence of a character in a string. Syntax: int strrpos(string haystack,char needle);

This function is used to find the last position where the character needle appears in the string haystack. Note that needle can only be one character, Chinese characters, etc. are not suitable. If the specified character is not found, a false value is returned.

strpos finds the first occurrence of a character in a string.

Syntax: int strpos(string haystack,string needle, int [offset]);

This function is used to find the position where the character needle first appears in the string haystack. Note that needle can only be one character, Chinese characters, etc. are not suitable. If the specified character is not found, a false value is returned. The offset parameter can be omitted, and y is used to start searching from offset.

strspn Find the number of times a string falls within the mask of another string.

Syntax: int strspn(string str1,string str2);

This function uses the str2 string as a mask and can be used to calculate how many characters in the str1 string fall within the str2 mask. Detailed explanation of PHP string comparison and search methods PHP function usage reference for comparing string similarity Introduction to the usage of PHP string comparison functions strcmp() and strcasecmp() PHP example code for comparing string similarity



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn