English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Experience notes
Main text
This article analyzes the method of retrieving strings in PHP. Share it with everyone for reference, as follows:
In PHP, there are many functions provided for searching strings, PHP can also implement the search function of strings like Word.
Application of strstr() function to retrieve specified keywords
Get the substring from the first occurrence of a specified string in another string to the end of the latter. If the operation is successful, it returns the remaining string (there are matching characters); if no matching characters are found, it returns false.
Syntax is as follows:
string strstr (string haystack,string needle)
The parameter haystack is a required parameter, used to specify the string from which to search. The parameter needle is a required parameter, used to specify the object to be searched. If the number is a numeric value, then it will search for the character that matches the ASCII value of this number.-Note: This function distinguishes between uppercase and lowercase letters/header("Content",-8"); Type: text51echo strstr("jb", "html; charset=utf", echo "<br>"; var_dump(strstr("jb", "Scream tutorial","This");51Scream tutorial","5"); echo "<br>"; echo strstr("https:", "");//www.oldtoolbag.com","w");
The running result is:
Ben Zhi Zhou bool(false) www.oldtoolbag.com
Note: The strrchr() function is exactly the opposite, starting from the end of the string and cutting off from the beginning of the first occurrence found.
echo strrchr("https://www.oldtoolbag.com","w");
The output result is:
w.jb51.net
Application of substr_count() function to search for the number of occurrences of a substring
int substr_count(string haystack,string needle)
The parameter haystack refers to the specified string, and the parameter needle refers to the specified character.
For example:
$str="123453336"; echo substr_count($str,"3");
The running result is:4
Hint: On the surface, the function of this function is to get the number of occurrences of a specified character in a string, and the output is just a number. However, in actual application, by judging the output number, different functions can be realized.
Tip: The search for the number of occurrences of a substring is generally used in search engines, which counts the number of occurrences of a substring in a string for the convenience of users to master the number of occurrences of the substring in the character as soon as possible.
Readers who are interested in more content related to PHP can check the special topics on this site: 'Summary of Usage of PHP String (string)', 'PHP Data Structures and Algorithms Tutorial', 'Summary of PHP Program Design Algorithms', 'Summary of PHP Sorting Algorithms', 'Summary of Common Traversal Algorithms and Techniques in PHP', 'Summary of Mathematical Operation Techniques in PHP', 'Comprehensive Manual of PHP Array (Array) Operations', and 'Summary of Common Database Operation Techniques in PHP'.
I hope the content described in this article will be helpful to everyone's PHP program design.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)