Two methods: 1. Use the "substr_replace($str,"",strpos($str,"specified character") 1)" statement to replace the content after the specified character position with a null character; 2 , use the "substr($str,0,strpos($str,"specified character") 1)" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php remove specified characters Two methods for the following content
Method 1: Using strpos() and substr_replace() functions
Use strpos () Find the position of the specified character. For example, the character d
<?php header('content-type:text/html;charset=utf-8'); $str = "abcdefg"; echo "原字符串:".$str."<br>"; $index = strpos($str,"d"); $res = substr_replace($str,"",$index+1); echo "去除字符d后的内容:".$res; ?>
Method 2: Use strpos() and substr() functions
<?php header('content-type:text/html;charset=utf-8'); $str = "abcdefg"; echo "原字符串:".$str."<br>"; $index = strpos($str,"e"); $res = substr($str,0,$index+1); echo "去除字符e后的内容:".$res; ?>
PHP Video Tutorial"
The above is the detailed content of How to remove content after specified characters in php. For more information, please follow other related articles on the PHP Chinese website!