php刪除字符串中某一元素的方法:1、用str_replace(),語(yǔ)法“str_replace("需要?jiǎng)h除的值","",$str)”;2、用str_ireplace(),語(yǔ)法“str_ireplace("要?jiǎng)h除的值","",$str)”。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php中刪除字符串中的某一元素
方法1:使用str_replace()函數(shù)
str_replace() 函數(shù)以其他字符替換字符串中的一些字符(區(qū)分大小寫);當(dāng)替換值為空字符串""
時(shí),即可實(shí)現(xiàn)刪除指定值。
語(yǔ)法:
str_replace(find,replace,string,count)
參數(shù) | 描述 |
---|---|
find | 必需。規(guī)定要查找的值。 |
replace | 必需。規(guī)定替換 find 中的值的值。 |
string | 必需。規(guī)定被搜索的字符串。 |
count | 可選。對(duì)替換數(shù)進(jìn)行計(jì)數(shù)的變量。 |
示例:
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello,world,Hello,world'; echo "原字符串:".$str."<br>"; $search = 'hello'; $replace = ''; echo "需要?jiǎng)h除的元素:".$search."<br>"; echo "刪除指定值的字符串:".str_replace($search, $replace, $str); ?>
方法2:使用str_ireplace()函數(shù)
str_ireplace() 函數(shù)替換字符串中的一些字符(不區(qū)分大小寫);同樣,當(dāng)替換值為空字符串""
時(shí),即可實(shí)現(xiàn)刪除指定值。
str_ireplace() 函數(shù)和str_replace() 函數(shù)的語(yǔ)法相似,可參考上面。
示例:
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello,world,Hello,world'; echo "原字符串:".$str."<br>"; $search = 'hello'; $replace = ''; echo "需要?jiǎng)h除的元素:".$search."<br>"; echo "刪除指定值的字符串:".str_ireplace($search, $replace, $str); ?>
推薦學(xué)習(xí):《PHP視頻教程》