php method to delete a string between specified characters: 1. Create a PHP sample file; 2. Enter the code "public function getStr(){...}"; 3. Delete the string through the getStr method Just specify the character between the two characters.
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to delete a string between specified characters in php?
PHP deletes the characters between the two specified characters in the string
public function getStr() { //str是一串有规则的字符串,现在我想删除其中一段,代码如下 $str="1,2018-04-15_20:15:11,2,2018-04-15_20:15:11,3,2018-04-15_20:15:11,4,2018-04-15_20:15:11"; //获取开始位置,选择 ,2,(防止和时间的年份冲突) $i1=strpos($str,',2,');//开始位置 $i2=strpos($str,',',$i1+3);//结束位置 $str1=substr($str,0,$i1) . substr($str,$i2,strlen($str)); echo $str."<br>".$str1;//换行输出,对比结果 //输出结果 //1,2018-04-15_20:15:11,2,2018-04-15_20:15:11,3,2018-04-15_20:15:11,4,2018-04-15_20:15:11 //1,2018-04-15_20:15:11,3,2018-04-15_20:15:11,4,2018-04-15_20:15:11 //OK,完美。 }
public function getStr($str,$str1,$n1,$str2,$n2) { /** * $str为要处理的字符串 * $str1为选中的唯一字符串 * $n1为从$str中第几个字符串开始截取 * $str2为选中的唯一字符串 * $n2为从$str2中第几个字符串结束截取 */ $i1=strpos($str,$str1)+$n1; $i2=strpos($str,$str2,$i1)+$n2; $str1=substr($str,0,$i1-1) . substr($str,$i2,strlen($str)); return $str1; }
//对应的五个值如下 $str="1,2018-04-15_20:15:11,2,2018-04-15_20:15:11,3,2018-04-15_20:15:11,4,2018-04-15_20:15:11"; $str1=",2,"; $n1="2"; //从指定值中的2 开始截取 $str2=","; $n2="1"; //截取到指定值中的第一位,也就是, echo(getStr($str,$str1,$n1,$str2,$n2)); //嗯,结果也一样,就是感觉没弄好,也不简单哈
PS: I met it, let me give you a summary
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to delete string between specified characters in php. For more information, please follow other related articles on the PHP Chinese website!