首頁  >  文章  >  後端開發  >  PHP substr_replace()

PHP substr_replace()

PHPz
PHPz原創
2024-08-29 12:50:50487瀏覽

substr_replace() 是 PHP 的另一個內建函數,用於將字串的一部分替換為另一個字串。基於需要傳遞必須完成字串替換的索引的輸入參數。我們還可以提供需要完成替換的索引的長度。要替換每個字串,我們可以提供一個字串陣列作為函數輸入參數的一部分。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP substr_replace() 語法

以下是文法:

文法:

substr_replace($str, $replace, $st, $len)

如上面的語法所示,函數接受 4 個參數,其中前 3 個參數是必需的,最後一個參數是可選的。它們如下:

1。 $str: 這是強制參數,這是必須進行替換的輸入字串。

2。 $replace: 這是另一個強制參數,這是替換 $str 參數的輸入字串。

3。 $st: 這也是必填參數,表示需要開始替換的索引位置。

  • 如果 $st 參數為正數,則從輸入字串開頭的給定位置開始替換。
  • 如果 $st 參數為負數,則從輸入字串末尾給定的位置開始替換。
  • 如果此 $st 參數為 0,則從指定字串的第一個字元開始替換。

4。 $len: 這是一個可選參數,它為我們提供了應該替換的字元數。如果沒有給出這個 $len 參數,那麼替換將自動停止在 $str 的末尾。

  • 這個參數描述了$str中如果$len為正規需要替換的那一段的長度。
  • 這為我們提供了當 $len 為負數時需要從字串末尾停止替換的字元總數。
  • 如果 $len 值為 0,則執行替換而不是插入。

傳回值:傳回值是依照上述參數替換後產生的字串。如果情況是字串數組,則傳回整個數組。

實作 PHP substr_replace() 的範例

以下是提到的範例:

範例#1

代碼:

<?php
echo substr_replace("Example for ","substring",8);
?>

輸出:

PHP substr_replace()

解釋: 在這個基本範例中,我們可以看到,透過使用substr_replace 函數,我們將第3 個參數給出的確切位置處的第一個字串「Example for」替換為“substring”,即第8個位置。因此,在第 8 個位置之後的輸出中,單字「for」被「substring」單字取代。

範例#2

代碼:

<?php
echo substr_replace("Example for","substring",-3);
?>

輸出:

PHP substr_replace()

說明:在此範例中,我們展示了位置參數中 -3 的功能。這個減號表示函數應該從後面開始替換單字。因此,這開始替換第二個參數來代替從第三個位置開始的第一個字串,從向後開始計數。

範例#3

代碼:

<?php
$replace = array("1: Try","2: Try","3: Try two");
echo implode("\n",substr_replace($replace,'Done',3,3));
?>

輸出:

PHP substr_replace()

說明:在此範例中,我們一次替換數組中宣告的多個字串。使用 substr_replace 函數,我們將前 3 個位置中的單字「Try」替換為單字「Done」。

範例#4

代碼:

<?php
echo substr_replace("Example insert", "to ", 8, 0);
?>

輸出:

PHP substr_replace()

說明:在這個範例中,我們展示如何使用 substr_replace 來執行插入,因為替換參數設定為 0。因此,在這個例子中,只發生插入,並且會出現沒有替換輸出中所示的字串。第二個參數「to」將插入到指定的位置,也就是從第 8 個位置開始。

範例#5

代碼:

<?php
echo substr_replace("dress", "gu", 0, 2);
?>

輸出:

PHP substr_replace()

Explanation: In this example, we will specify the length parameter as 2 hence the substr_replace will start replacing the input string starting from the $start parameter which is 0 until 2. Hence from the word “dress”, the first 2 letters will be replaced by “gu” hence becoming “guess”.

Example #6

Code:

<?php
$str = 'Example:/Replace/';
echo "First: $str\n";
// The below 2 displayy the replacement of our input string with 'test'
echo substr_replace($str, 'test', 0) . "\n";
echo substr_replace($str, 'test', 0, strlen($str)) . "\n";
// Here we are inserting the word test at the starting of the string
echo substr_replace($str, 'test', 0, 0) . "\n";
// The below 2 will replace the string "Replace" from input string in $str with 'test'
echo substr_replace($str, 'test', 9, -1) . "\n";
echo substr_replace($str, 'test', -6, -1) . "\n";
// Here we are deleting "Replace" from the input string
echo substr_replace($str, '', 7, -1) . "\n";
?>

Output:

PHP substr_replace()

Explanation: In this example, we are first declaring an input string $str upon which all the operations of the function are going to be performed. In the first part of operation, we are replacing the string completely with the given string as we are specifying both starts as 0. In the second part, we are replacing the “Replace” of the input string with the ‘test’.

Example #7

Code:

<?php
$ip = array('1: PPP', '2: RRR', '3: SSS');
// A basic example where we are replacing all 3 letters of input string with TTT
echo implode('; ', substr_replace($ip, 'TTT', 3, 3))."\n";
// Another case where we are replacing the input string with the different inputs as given below
$replace = array('DDD', 'EEE', 'FFF');
echo implode('; ', substr_replace($ip, $replace, 3, 3))."\n";
// In this case we are replacing all the input characters with 3 different characters
$length = array(1, 2, 3);
echo implode('; ', substr_replace($ip, $replace, 3, $length))."\n";
?>

Output:

PHP substr_replace()

Explanation: In this example, we are showing the use of substr_replace function for replacing multiple strings as shown in input $ip.

Conclusion

Hence this function is used for replacing one string with another string based on the parameters specified. The same can also be used for inserting strings as required when the $start and $length parameter are given in 0 or negative values.

以上是PHP substr_replace()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP preg_split()下一篇:PHP preg_split()