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

PHP preg_replace()

PHPz
PHPz原創
2024-08-29 12:49:57542瀏覽

PHP preg_replace() 函數是 PHP 程式語言中的內建函數。 Preg_replace() 函數有助於執行正規表示式來搜尋和取代內容。可以對數組索引值內部的單一字串或多個字串元素進行替換。它在某種程度上類似於 preg_match(),但在 preg_replace() 中,在對字串執行模式匹配後,將對特定文字替換字串元素。這是 PHP 程式語言中用於操作字串內容的重要正規表示式之一。下面您將詳細了解 preg_replace()。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

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

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

文法

文法如下:

preg_replace( $pattern1, $replacement1, $subject1, $limit1, $count1 )

參數

preg_replace() 函數通常主要接受上面提到的五個參數。它們是 $pattern1、$replacement1、$subject1、$limit1 和 $count1 參數。

了解這些參數,並在下面簡要說明。

  • $pattern1:preg_replace() 函數的 $pattern1 參數包含字串元素,有助於搜尋內容。它可以是單一字串值,也可以是字串陣列。
  • Sreplacement1: preg_replace() 函數的$replacement1 參數是強制參數。指定字串或帶有一些要替換的字串的字串陣列會很有幫助。如果此參數具有字串值,則pattern1參數將是一個數組,並且所有模式將被該特定字串取代。如果 replacement1 和pattern1 參數都是數組,則每個pattern1 都會被replacement1 對應項取代。如果 replacement1 是數組,而且它包含的元素少於 pattern1 數組,那麼任何額外的模式都會被簡單的空字串替換。
  • $subject1:preg_replace() 函數的 $subject1 參數是一個字串或字串陣列,用於從 $pattern 變數的字串值中搜尋並替換字串內容。
  • $limit1:preg_replace() 函數的 $limit 參數非常重要,它指定每個模式最多可能的替換。
  • $count1:preg_replace() 函數的 $count 參數通常是可選參數。此參數的變數值以數字填充,該數字表示在原始字串中完成的替換次數。

preg_replace() 函數的傳回值:只有當 subect1 參數是陣列/字串時,preg_replace() 函數才會傳回陣列。

PHP preg_replace() 函數如何運作?

PHP 程式語言的 Preg_replace() 函數是基於其中提到的參數來運作。它主要透過使用搜尋時使用的原始字串和替換字串來工作。其他參數可能沒那麼重要。 Preg_replace() 將採用正規表示式作為參數(第一個參數),用於將每個匹配項替換為 2nd 參數。第二個參數是純文本,而 3rd 參數是要工作的字串。第三個參數可以包含 $n ,它是插入作為正規表示式規則的一部分相符的文字。如果我們正在編寫一些複雜的多部分正規表示式。您可以使用 $0 來使用符合的文字。請參閱範例,以便您在下面的範例 2 中了解。

在 PHP 中實作 preg_replace() 函數的範例

以下是一些提到的例子:

範例#1

在下面的範例中,使用值“Copyright 1998”建立變數“$copy_date1”,然後使用 preg_replace() 函數作為值建立相同的變數。因此 preg_replace() 函數的結果將儲存在「$copy_date1」變數中。在 preg_replace() 函數中,值 0-9(全部)將被字串值「2020」取代並儲存在「$copy_date1」變數中。然後將列印“$copy_date1”中存在的值,即更改後的字串。然後“Copyright 2020”將列印在輸出中。

代碼:

<?php
$copy_date1 = "Copyright 1998";
$copy_date1 = preg_replace("([0-9]+)", "2020", $copy_date1);
print $copy_date1;
?>

輸出:

PHP preg_replace()

Example #2

In the below example, a variable “$a1” is created with the string value “Fool bool tool fool”. Then again the same naming variable is created with the preg_replace() function. In the preg_replace() string value is used to search which are ending with a cool after any alphabet. If there is anything then those string values will be printed one by one after the “Got word:” word. And this result will be stored in the “$a1” variable. Then the word will be printed using the print function.

Code:

<?php
$a1 = "Foo mool bool tool fool";
$a1 = preg_replace("/[A-Za-z]ool\b/", "Got word: $0 \n", $a1);
print $a1;
?>

Output:

PHP preg_replace()

Example #3

In the below example, the date will be changed in the string value as mentioned in the replacement1 term. At first a variable “$date1” is created inside of the PHP tags with the value “Sep 29, 2020”. Then the “$pattern1” variable is created with meta-characters that are used to find a word in the character. Then the “$replacement1” variable is created with the value ”${1} 6, $3”. Here “1” inside flower braces will remain the same and then in the 2nd value 6 will be replaced in the place of 29 and $3 represents the 3 string elements inside of the array. If “$1 ” is placed then only Sep will be printed if the $2 is represented then replaced value only 6 will be printed. So the $3 is mentioned to mention all the 3 elements which are to be printed using echo function.

Code:

<?php
$date1 = 'Sep 29, 2020';
$pattern1 = '/(\w+) (\d+), (\d+)/i';
$replacement1 = '${1} 6,$3';
echo preg_replace($pattern1, $replacement1, $date1);
?>

Output:

PHP preg_replace()

Example #4

In the below example, multiple string elements are replaced using the array function with the single string values in the index values to change. At first “string1” variable is created with a string sentence with many words. Then “$patterns1” variable is created with array() function as variable. Then inside of the $patterns[index value] is/are stored with a different string that is already present in the original string. Similarly the same is done for the “$replacements1” variable with different string values to change the list of strings inside of the string sentence. Then “$newstr1” variable is created with the value as preg_replace() function. Then echo function is used to print the changed string sentence. Check out this in the output image.

Code:

<?php
$string1 = 'The fast black tiger runs away from the zoo.';
$patterns1 = array();
$patterns1[0] = '/fast/';
$patterns1[1] = '/black/';
$patterns1[2] = '/tiger/';
$replacements1 = array();
$replacements1[2] = 'Slow';
$replacements1[1] = 'Elephant';
$replacements1[0] = 'Jumped and';
$newstr1 = preg_replace($patterns1, $replacements1, $string1);
echo "<b>The String after the replacements:</b> " .$newstr1;
?>

Output:

PHP preg_replace()

Conclusion

I hope you understood what is the definition of PHP preg_replace() function with its syntax along with various parameter explanation, How preg_replace() function works along with the various examples to implement preg_replace() function in PHP Programming language.

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

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