新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
php怎么進(jìn)行字符串替換
本教程操作環(huán)境:windows7系統(tǒng)、php7.1版、DELL G3電腦

在 PHP 中,可以對一個字符串中的特定字符或子串進(jìn)行替換,這是非常常用的功能。
php進(jìn)行字符串替換
方法1:str_ireplace() 和 str_replace() 函數(shù)
str_ireplace() 和 str_replace 使用新的字符串替換原來字符串中指定的特定字符串,str_replace 區(qū)分大小寫,str_ireplace() 不區(qū)分大小寫,兩者語法相似。
str_replace(find,replace,string,count) str_ireplace(find,replace,string,count)
| 參數(shù) | 描述 |
|---|---|
| find | 必需。規(guī)定要查找的值。 |
| replace | 必需。規(guī)定替換 find 中的值的值。 |
| string | 必需。規(guī)定被搜索的字符串。 |
| count | 可選。對替換數(shù)進(jìn)行計數(shù)的變量。 |
示例:
"; $search = 'hello'; $replace = 'hi'; echo "
替換字符串:
"; echo str_replace($search, $replace, $str)."
"; echo str_ireplace($search, $replace, $str)."
"; ?>
方法2:substr_replace() 函數(shù)
substr_replace() 函數(shù)把字符串的一部分替換為另一個字符串。
substr_replace() 函數(shù)的語法如下:
substr_replace(string,replacement,start,length)
| 參數(shù) | 描述 |
|---|---|
| string | 必需。規(guī)定要檢查的字符串。 |
| replacement | 必需。規(guī)定要插入的字符串。 |
| start | 必需。規(guī)定在字符串的何處開始替換。
|
| length | 可選。規(guī)定要替換多少個字符。默認(rèn)是與字符串長度相同。
|
示例:
以上代碼的執(zhí)行結(jié)果為:
hi,world,hello,world
網(wǎng)站欄目:php怎么進(jìn)行字符串替換
本文網(wǎng)址:http://m.fisionsoft.com.cn/article/djhohsh.html


咨詢
建站咨詢
