Home >php教程 >php手册 >PHP实现删除字符串中任何字符的函数

PHP实现删除字符串中任何字符的函数

WBOY
WBOYOriginal
2016-06-06 19:48:321049browse

这篇文章主要介绍了PHP实现删除字符串中任何字符的函数,涉及php针对字符串的遍历与截取操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了PHP实现删除字符串中任何字符的函数。分享给大家供大家参考。具体如下:

function delStr($start, $end, $orgenStr) { //读取要删除字符位置的前一部分字符串,并赋值给$temp //strpos读取字符第一次出现的位置 //substr读取指定开始与结束位置的子字符串 //echo $before."—". $last; $temp=$orgenStr; while(strpos($temp, $start) && strpos($temp, $end)){ $temp=substr($temp, 0, strpos($temp, $start)).substr($temp,strpos($temp, $end)+strlen($end));; //读取要删除字符位置的后一部分字符串,然后将前后部分连接,并赋值给$temp //返回最后是字符串 } return $temp; } //应用实例 $a="aaaa12345678bbbbtttttttttttttttttttttaaaa12345678bbbb kkkkkkkkkkkkaaaa12345678bbbbttttttttttttttttttttt"; $b="1234"; $c="5678"; echo delStr($b,$c,$a);

输出为:

aaaabbbbtttttttttttttttttttttaaaabbbbkkkkkkkkkkkkaaaabbbbttttttttttttttttttttt

PS:

一般应用在1234和5678中间有动态内容,可以批量地删除

update:加入了循环删除,,可以删除所有符合条件的字符串 .

希望本文所述对大家的php程序设计有所帮助。

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