搜尋
首頁後端開發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
超越炒作:評估當今PHP的角色超越炒作:評估當今PHP的角色Apr 12, 2025 am 12:17 AM

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

PHP中的弱參考是什麼?什麼時候有用?PHP中的弱參考是什麼?什麼時候有用?Apr 12, 2025 am 12:13 AM

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

解釋PHP中的__ Invoke Magic方法。解釋PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

解釋PHP 8.1中的纖維以進行並發。解釋PHP 8.1中的纖維以進行並發。Apr 12, 2025 am 12:05 AM

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區:資源,支持和發展PHP社區:資源,支持和發展Apr 12, 2025 am 12:04 AM

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP與Python:了解差異PHP與Python:了解差異Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

php:死亡還是簡單地適應?php:死亡還是簡單地適應?Apr 11, 2025 am 12:13 AM

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來:改編和創新PHP的未來:改編和創新Apr 11, 2025 am 12:01 AM

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),