Home >Backend Development >PHP Problem >How to remove all characters after specified characters in php
Method: 1. Use the strpos() function to find the position of the specified character. The syntax is "strpos(string, specified character)"; 2. Use the substr() function to remove all characters after the specified character. The syntax is It is "substr(string,0,specified character position 1)".
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
In php, first find the position of the specified character, and then use the method of intercepting the string to remove the content after a certain string
Create a new php file, named test.php, to explain how php can achieve the content after removing a certain string. Use the header() method to set the encoding format of the page to utf-8.
Create a string and use the strpos function to find the position of the specified character, for example, find the position of the d character.
Use the substr function to intercept from the beginning of the string to the position of character d (because the position starts from zero, so 1 must be added), thereby removing the content after a certain string.
#In the test.php file, use echo to output the obtained string. View Results.
Summary:
1. Create a string and use the strpos function to find the position of the specified character, for example, find the position of the d character.
2. Use the substr function to intercept from the beginning of the string to the position of character d (because the position starts from zero, so 1 must be added), thereby removing the content after a certain string.
3. Use echo to output the obtained string.
4. Open the test.php file in the browser and view the results.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove all characters after specified characters in php. For more information, please follow other related articles on the PHP Chinese website!