Home  >  Article  >  Backend Development  >  How to remove all characters after specified characters in php

How to remove all characters after specified characters in php

WBOY
WBOYOriginal
2022-03-09 11:20:493595browse

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)".

How to remove all characters after specified characters in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

How to remove all characters after the specified character in php

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.

How to remove all characters after specified characters in php

#In the test.php file, use echo to output the obtained string. View Results.

How to remove all characters after specified characters in php

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!

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
Previous article:Can PHP write blockchain?Next article:Can PHP write blockchain?