目錄
PHP substr_replace() 語法
實作 PHP substr_replace() 的範例
範例#5
Example #7
Conclusion
首頁 後端開發 php教程 PHP substr_replace()

PHP substr_replace()

Aug 29, 2024 pm 12:50 PM
php

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1677
14
CakePHP 教程
1431
52
Laravel 教程
1334
25
PHP教程
1280
29
C# 教程
1257
24
如果session_start()被多次調用會發生什麼? 如果session_start()被多次調用會發生什麼? Apr 25, 2025 am 12:06 AM

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

作曲家:通過AI的幫助開發PHP 作曲家:通過AI的幫助開發PHP Apr 29, 2025 am 12:27 AM

AI可以幫助優化Composer的使用,具體方法包括:1.依賴管理優化:AI分析依賴關係,建議最佳版本組合,減少衝突。 2.自動化代碼生成:AI生成符合最佳實踐的composer.json文件。 3.代碼質量提升:AI檢測潛在問題,提供優化建議,提高代碼質量。這些方法通過機器學習和自然語言處理技術實現,幫助開發者提高效率和代碼質量。

session_start()函數的意義是什麼? session_start()函數的意義是什麼? May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

如何使用MySQL的函數進行數據處理和計算 如何使用MySQL的函數進行數據處理和計算 Apr 29, 2025 pm 04:21 PM

MySQL函數可用於數據處理和計算。 1.基本用法包括字符串處理、日期計算和數學運算。 2.高級用法涉及結合多個函數實現複雜操作。 3.性能優化需避免在WHERE子句中使用函數,並使用GROUPBY和臨時表。

H5:HTML5的關鍵改進 H5:HTML5的關鍵改進 Apr 28, 2025 am 12:26 AM

HTML5帶來了五個關鍵改進:1.語義化標籤提升了代碼清晰度和SEO效果;2.多媒體支持簡化了視頻和音頻嵌入;3.表單增強簡化了驗證;4.離線與本地存儲提高了用戶體驗;5.畫布與圖形功能增強了網頁的可視化效果。

作曲家:PHP開發人員的軟件包經理 作曲家:PHP開發人員的軟件包經理 May 02, 2025 am 12:23 AM

Composer是PHP的依賴管理工具,通過composer.json文件管理項目依賴。 1)解析composer.json獲取依賴信息;2)解析依賴關係形成依賴樹;3)從Packagist下載並安裝依賴到vendor目錄;4)生成composer.lock文件鎖定依賴版本,確保團隊一致性和項目可維護性。

MySQL的字符集和排序規則如何配置 MySQL的字符集和排序規則如何配置 Apr 29, 2025 pm 04:06 PM

在MySQL中配置字符集和排序規則的方法包括:1.設置服務器級別的字符集和排序規則:SETNAMES'utf8';SETCHARACTERSETutf8;SETCOLLATION_CONNECTION='utf8_general_ci';2.創建使用特定字符集和排序規則的數據庫:CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci;3.創建表時指定字符集和排序規則:CREATETABLEexample_table(idINT

怎樣在C  中使用type traits? 怎樣在C 中使用type traits? Apr 28, 2025 pm 08:18 PM

typetraits在C 中用於編譯時類型檢查和操作,提升代碼的靈活性和類型安全性。 1)通過std::is_integral和std::is_floating_point等進行類型判斷,實現高效的類型檢查和輸出。 2)使用std::is_trivially_copyable優化vector拷貝,根據類型選擇不同的拷貝策略。 3)注意編譯時決策、類型安全、性能優化和代碼複雜性,合理使用typetraits可以大大提升代碼質量。

See all articles