PHP String str_replace() 函數(shù)
str_replace() 函數(shù)是PHP的一個內(nèi)置函數(shù),它是一個 區(qū)分大小寫 的函數(shù),用于將字符串中的某些字符替換為其他字符。愛掏網(wǎng) - it200.com它用于將所有的搜索字符串的出現(xiàn)替換為替換字符串。愛掏網(wǎng) - it200.com
str_replace() 函數(shù)的語法如下所示,它具有以下四個參數(shù)。愛掏網(wǎng) - it200.com
str_replace ( search,replace, string,count)
此函數(shù)在工作時遵循一些規(guī)則,如下所示:
- 如果要搜索的字符串是一個數(shù)組,則返回一個數(shù)組。愛掏網(wǎng) - it200.com
- 如果要搜索的字符串是一個數(shù)組,則對數(shù)組的每個元素進行搜索和替換。愛掏網(wǎng) - it200.com
- 如果
$search
和$replace
都是數(shù)組,并且$replace
的元素少于$search
數(shù)組,則使用空字符串作為替換。愛掏網(wǎng) - it200.com - 如果
$search
是一個數(shù)組,但$replace
是一個字符串,則替換字符串將用于每個搜索值。愛掏網(wǎng) - it200.com
參數(shù)
str_replace()函數(shù)有四個參數(shù),其中三個是必需的,另一個是可選參數(shù)。愛掏網(wǎng) - it200.com以下是所有這些參數(shù)的詳細說明:
$search(必需) – 此參數(shù)是一個 必需 參數(shù),可以是字符串或數(shù)組類型的值。愛掏網(wǎng) - it200.com$search
參數(shù)包含要在$string
中進行替換的值。愛掏網(wǎng) - it200.com
$replace(必需) – 此參數(shù)是一個 必需 參數(shù),將被搜索值替換。愛掏網(wǎng) - it200.com簡單來說 – 此參數(shù)保存將替換$string
中$search
值的值。愛掏網(wǎng) - it200.com
$string(必需) – 此參數(shù)也是一個 必需 參數(shù),它是一個數(shù)組或字符串,其中搜索和替換值被搜索和替換。愛掏網(wǎng) - it200.com這是我們要處理的字符串或數(shù)組。愛掏網(wǎng) - it200.com
$count(必需) – 它是最后一個 可選 參數(shù)。愛掏網(wǎng) - it200.com它是一個整數(shù)變量,用于計算字符串中進行的替換次數(shù)。愛掏網(wǎng) - it200.com簡單地說,此變量存儲了字符串$string
上執(zhí)行的替換總數(shù)。愛掏網(wǎng) - it200.com
返回值
此函數(shù)返回一個基于$string
參數(shù)的替換值的數(shù)組或字符串。愛掏網(wǎng) - it200.com
重要技術(shù)細節(jié)
返回值 | 它返回一個包含替換后值的字符串或數(shù)組。愛掏網(wǎng) - it200.com |
---|---|
支持的PHP版本 | PHP 4及以上版本支持該函數(shù)。愛掏網(wǎng) - it200.com |
更新日志 | PHP 5.0中包含了$count 參數(shù)。愛掏網(wǎng) - it200.com 在PHP 4.3.3之前,當使用$search和$replace 參數(shù)作為數(shù)組時,該函數(shù)遇到了許多問題。愛掏網(wǎng) - it200.com因此,空的$search 索引會被跳過,而不會在$replace 數(shù)組上推進內(nèi)部指針。愛掏網(wǎng) - it200.com新版本已解決了這個問題。愛掏網(wǎng) - it200.com 在PHP 4.0.5之后,大多數(shù)參數(shù)現(xiàn)在都可以是一個數(shù)組。愛掏網(wǎng) - it200.com |
示例
這是str_replace()函數(shù)的實際應(yīng)用。愛掏網(wǎng) - it200.com
示例1: 使用字符串變量的基本示例
<?php
string = "Hii everyone!";search = 'Hii';
replace = 'Hello';
echo '<b>'."String before replacement:".'</br></b>';
echostring.'</br>';
newstr = str_replace(search, replace,string, count);
echo '<b>'."New replaced string is:".'</br></b>';
echonewstr.'</br>';
echo 'Number of replacement ='.$count;
?>
輸出:
在上面的示例中,我們可以看到”Hii”被替換為”Hello”,替換次數(shù)只有1次。愛掏網(wǎng) - it200.com
示例2: 使用數(shù)組變量替換
要在$string
中替換多個值,我們需要使用一個數(shù)組來存儲這些替換值。愛掏網(wǎng) - it200.com
<?php
string = "Hii everyone! welcome to javaTpoint website. We will get best technical content here.";search = array("Hii", "We");
replace = array("Hello", "You");
echo '<b>'."String before replacement:".'</br></b>';
echostring.'</br>';
newstr = str_replace(search, replace,string, count);
echo '<b>'."New replaced string is:".'</br></b>';
echonewstr.'</br>';
echo 'Number of replacement ='.$count;
?>
輸出:
在這個輸出中,我們可以看到 “Hii” 被替換成了 “Hello”,”We”被替換成了 “You”,替換的數(shù)量為2。愛掏網(wǎng) - it200.com
示例3: 元音字母替換為空字符串
<?php
string = "Apple is my favorite fruite.";search = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
echo '<b>'."String before replacement:".'</br></b>';
echo string.'</br>';newstr = str_replace(search, '',string, count);
echo '<b>'."New replaced string is:".'</br></b>';
echonewstr.'</br>';
echo 'Number of replacement ='.$count;
?>
輸出:
在這個示例中,我們將元音字母(包括大寫和小寫)替換為空字符串。愛掏網(wǎng) - it200.com在這個字符串中,進行了10次替換。愛掏網(wǎng) - it200.com
示例4: 區(qū)分大小寫
str_replace 是一個區(qū)分大小寫的函數(shù),在下面的示例中得到證明。愛掏網(wǎng) - it200.com
<?php
string = "Hello world!";
printf(str_replace("hello", "Hii",string)); //Does not replace the string
echo '</br>';
printf(str_replace("Hello", "Hii", $string)); //Hello will replace with Hii
?>
輸出:
在這個示例中,”Hello”沒有被替換為”Hii”,因為搜索字符串是”hello”。愛掏網(wǎng) - it200.com在第二種情況下,”Hello”被替換為”Hii”,因為這里的搜索字符串是”Hello”,與字符串匹配。愛掏網(wǎng) - it200.com這證明str_replace()函數(shù)是 區(qū)分大小寫 的。愛掏網(wǎng) - it200.com
Hello world!
Hii world!