Home > Article > Backend Development > How to remove the content after a certain character in php
Method: 1. Use strpos() to find the position of the specified character, syntax "strpos($str,"specified character")"; 2. Use substr() to intercept from the beginning of the string to the specified character Position (because the position starts from zero, so you need to add 1), the syntax is "substr($str,0, position value 1)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php remove a certain The content after a character
In PHP, you can use the strpos() and substr() functions to remove the content after a certain character
Implementation idea:
Use strpos function to find the position of the specified character, for example character d
Use substr function to intercept from the beginning of the string to character d position (because the position starts from zero, you must add 1)
Implementation example:
<?php header('content-type:text/html;charset=utf-8'); $str = "abcdefg"; $index = strpos($str,"d"); $res = substr($str,0,$index+1); echo $res; ?>
Extended knowledge: You can also use the strpos() and substr() functions to remove the content before a certain character
Just use the substr function to intercept the string starting from the specified character position (no need at this time Added 1)
<?php header('content-type:text/html;charset=utf-8'); $str = "abcdefg"; $index = strpos($str,"d"); $res = substr($str,$index); echo $res; ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to remove the content after a certain character in php. For more information, please follow other related articles on the PHP Chinese website!