PHP preg_replace()

PHPz
リリース: 2024-08-29 12:49:57
オリジナル
540 人が閲覧しました

PHP preg_replace() 関数は、PHP プログラミング言語の組み込み関数です。 Preg_replace() 関数は、コンテンツの検索と置換のための正規表現の実行に役立ちます。置換は、配列インデックス値内の単一の文字列または複数の文字列要素に対して実行できます。これはある程度まで preg_match() に似ていますが、 preg_replace() では文字列に対してパターン マッチを実行した後、特定のテキストに対して文字列要素の置換が行われます。これは、文字列コンテンツを操作するための PHP プログラミング言語の重要な正規表現の 1 つです。 preg_replace() については、以下で簡単に詳しく説明します。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文

以下の構文:

preg_replace( $pattern1, $replacement1, $subject1, $limit1, $count1 )
ログイン後にコピー

パラメータ

preg_replace() 関数は通常、上記のように主に 5 つのパラメータを受け入れます。これらは、$pattern1、$replacement1、$subject1、$limit1、$count1 パラメーターです。

これらのパラメータについては、以下の簡単な説明で理解してください。

  • $pattern1: preg_replace() 関数の $pattern1 パラメータには、コンテンツの検索に役立つ文字列要素が含まれています。単一の文字列値または文字列の配列のいずれかになります。
  • Sreplacement1: preg_replace() 関数の $replacement1 パラメーターは必須パラメーターです。文字列、または置換する文字列を含む文字列配列を指定すると便利です。このパラメータが文字列値を持つ場合、pattern1 パラメータは配列となり、すべてのパターンがその特定の文字列に置き換えられます。 replace1 パラメータと pattern1 パラメータの両方が配列の場合、すべての pattern1 は対応する replace1 に置き換えられます。 replace1 が配列で、その配列が pattern1 配列よりも少ない要素で構成されている場合、余分なパターンは単純な空の文字列に置き換えられます。
  • $subject1: preg_replace() 関数の $subject1 パラメーターは、$pattern 変数の文字列値から文字列の内容を検索して置換するために使用される文字列または文字列の配列です。
  • $limit1: preg_replace() 関数の $limit パラメータは非常に重要であり、最大で各パターンの可能な置換を指定します。
  • $count1: preg_replace() 関数の $count パラメータは通常、オプションのパラメータです。このパラメータの変数の値には、元の文字列で行われた置換の数を示す数値が入力されます。

preg_replace() 関数の戻り値: preg_replace() 関数は、subect1 パラメーターが配列/文字列の場合にのみ配列を返します。

PHP preg_replace() 関数はどのように機能しますか?

PHP プログラミング言語の Preg_replace() 関数は、内部で指定されたパラメーターに基づいて動作します。主に、検索時に使用される元の文字列と置換文字列を使用して機能します。他のパラメータはそれほど重要ではないかもしれません。 Preg_replace() は、各一致を 2nd パラメータと置換するために使用されるパラメータ (最初のパラメータ) であるため、正規表現を受け取ります。 2 番目のパラメータはプレーン テキストですが、3rd パラメータは動作する文字列です。 3 番目のパラメーターには、正規表現ルールの一部として一致したテキストを挿入する $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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!