PHP實現刪除字串中任何字元的函式
阿新 • • 發佈:2018-11-09
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);
輸出為: