Home >Backend Development >PHP Tutorial >Find characters in php string_PHP tutorial

Find characters in php string_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:211205browse

What this article is going to talk about is using the strstr and strpos functions to query whether there is the character content we want in the specified string. You can refer to it if you need it.

The strstr() function is used to obtain the substring from the first occurrence of a specified string in another string to the end of the latter. If the execution is successful, it returns the remaining string (matching characters exist); If no matching character is found, false is returned.
Syntax:
string strstr(string haystack,string needle)
Parameter haystack: necessary parameter, specify which string to search from.
Parameter needle: necessary parameter, specifying the search object. If the argument is a numeric value, then a search is performed for characters that match the ASCII value of the numeric value.
Example:

Grammar
The code is as follows
 代码如下 复制代码
echo strstr(“明日编程词典”,”编”); //输出查询的字符串
echo “
”;                      //执行回车
echo strstr(“www.111c.net”,”111”);  //输出查询的字符串(从第一个m取值)
echo “
”;                         //执行回车
echo strstr(“0431-84972266″,”8″);    //输出查询的字符串
?>
Copy code


echo strstr("Tomorrow's Programming Dictionary","Editor"); //Output the query string echo “
”; //Execute carriage return

echo strstr("www.111c.net","111"); //Output the query string (value from the first m)

echo “
”; //Execute carriage return

echo strstr(“0431-84972266″,”8″); //Output the query string
?>




Definition and Usage
The strpos() function returns the position of the first occurrence of a string within another string.

 代码如下 复制代码

echo strpos("Hello world!","wo");
?>输出:

6

If the string is not found, return false.
strpos(string,find,start) parameter description string required. Specifies the string to be searched for.

find required. Specifies the characters to search for.

start is optional. Specifies the location from which to start the search. Note: This function is case sensitive. For case-insensitive searches, use the stripos() function. Example
The code is as follows Copy code
echo strpos("Hello world!","wo"); ?>Output: 6
http://www.bkjia.com/PHPjc/631320.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631320.htmlTechArticleWhat this article is going to talk about is using the strstr and strpos functions to query whether there are the characters we want in the specified string. Content, you can refer to it if necessary. The strstr() function is used to get a pointer...
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