溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何將指定字符串替換為空

發(fā)布時(shí)間:2021-06-11 09:50:05 來(lái)源:億速云 閱讀:388 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹php如何將指定字符串替換為空,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

方法:1、使用str_ireplace(),語(yǔ)法“str_ireplace(指定子字符串, '', 原字符串)”;2、使用substr_replace(),語(yǔ)法“substr_replace(原字符串,'',開(kāi)始替換的位置,替換長(zhǎng)度)”。

本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

在PHP中,可以通過(guò)str_ireplace() 和 str_replace()函數(shù) 使用新的字符串替換原來(lái)字符串中指定的特定字符串,str_replace 區(qū)分大小寫(xiě),str_ireplace() 不區(qū)分大小寫(xiě)。

1、使用str_ireplace()函數(shù)替換字符串為空

str_ireplace() 的語(yǔ)法如下:

mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

該函數(shù)返回一個(gè)字符串或者數(shù)組。該字符串或數(shù)組是將 subject 中全部的 search 用 replace 替換(忽略大小寫(xiě))之后的結(jié)果。參數(shù) count 表示執(zhí)行替換的次數(shù)。

示例:替換字符串為空

<?php
$str = 'hello,world,hello,world';
$replace = '';
$search = 'hello';
echo str_ireplace($search, $replace, $str);
?>

輸出:

,world,,world

2、使用substr_replace() 函數(shù)替換字符串為空

substr_replace() 函數(shù)的語(yǔ)法如下:

mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )

substr_replace() 在字符串 string 的副本中將由 start 和可選的 length 參數(shù)限定的子字符串使用 replacement 進(jìn)行替換。

如果 start 為正數(shù),替換將從 string 的 start 位置開(kāi)始。如果 start 為負(fù)數(shù),替換將從 string 的倒數(shù)第 start 個(gè)位置開(kāi)始。

如果設(shè)定了 length 參數(shù)并且為正數(shù),就表示 string 中被替換的子字符串的長(zhǎng)度。如果設(shè)定為負(fù)數(shù),就表示待替換的子字符串結(jié)尾處距離 string 末端的字符個(gè)數(shù)。如果沒(méi)有提供此參數(shù),那么默認(rèn)為 strlen(string)(字符串的長(zhǎng)度)。當(dāng)然,如果 length 為 0,那么這個(gè)函數(shù)的功能為將 replacement 插入 string 的 start 位置處。

示例:替換字符串為空

<?php
$str = 'hello world!';
$replace = '';
echo substr_replace($str, $replace, 0,5);
?>

輸出:

world!

以上是“php如何將指定字符串替換為空”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI