PHP 爆発()

WBOY
リリース: 2024-08-29 12:55:29
オリジナル
930 人が閲覧しました

PHP プログラミング言語のexplode() 関数は、文字列を多くの異なる文字列に分割するのに役立つ組み込み関数です。 explode() 関数は、文字列の区切り文字に基づいて文字列を分割するのに役立ちます。つまり、区切り文字が存在する/出現する場所で文字列を分割します。このexplode()関数は、元の文字列を分割した後に形成された文字列を含む配列を返します。この PHPexplode() 関数は通常、文字列を多くの文字列要素に分割し、配列に格納するために 3 つのパラメータのみを受け入れます。それは、大きな紐を多数の小さな紐に切断するようなものです。

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

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

構文:

リーリー

PHP ではexplode() 関数はどのように動作しますか?

PHP プログラミング言語のexplode() 関数は、前述の 3 つのパラメーターに基づいて機能します。これらは、Separator1、OriginalString1、および No.ofElements1 パラメータです。これは、explode() 関数内のパラメータを使用して文字列を小さな文字列に分割することで機能します。これらの小さな文字列要素はすべて、インデックス値とともに配列に保存され、explode() 関数のパラメーターに基づいて保存されます。

パラメータの説明:

explode() 関数のパラメータは実際には 3 つのパラメータを受け入れますが、そのうちの 2 つのパラメータのみが必須であり、1 つのパラメータはオプションのパラメータです。

1. Separator1/Delimeter:PHP プログラミング言語の Separator1 パラメーターは、実際には文字列を分割する必要があるいくつかの重要なポイントを指定します。これは、文字列要素内に文字列文字が見つかると、その文字列が終わりを記号化することを意味します。 1 つの配列要素の開始点と別の配列要素の開始点。この区切り文字/区切り文字 1 パラメータは必須パラメータです。

2. OriginalString1:PHP プログラミング言語の OriginalString1 パラメーターは、実際には、文字列を多数の文字列に分割するために使用される元の文字列であり、文字列文字が文字列内で使用可能な場合にのみ配列に格納されます。この OriginalString1 パラメータも必須パラメータです。

3. No.ofElements1/Limit:このパラメータはオプションのパラメータであり、必須ではありません。このパラメータは、配列要素の数を指定するのに役立ちます。このパラメータには、任意のタイプの整数を使用できます。

正の整数、負の整数、またはゼロの整数を指定できます。

  • 正の整数 (N):No.ofElements1 パラメーターが正の整数値で渡された場合、配列にはこの数の文字列要素が含まれることを意味します。区切り文字を使用して要素を分離した後の要素の数が N-1 個の要素よりも大きい値として現れるが、同じままであり、最後の文字列要素が残りの文字列全体になります。
  • 負の整数 (N):負の整数が No.ofElements1 パラメーターに渡された場合、最後の文字列要素 N がトリミングされ、残りの配列要素が 1 つの単一文字列として返されます。
  • ゼロ整数:No.ofElements1 パラメーターが「ゼロ」値で渡された場合、文字列要素を 1 つだけ返す配列、つまり完全な文字列 (文字列全体)。この No.ofElements1 パラメータに値が渡されない場合、返される配列には、セパレータで文字列を区切った後に形成される要素の合計数が含まれます。

戻り値の型:

explode() 関数の戻り値の型は、文字列のリストを含む単一の配列です。

PHPexplode()の例

言及されている例を以下に示します:

例 #1

これは、文字列間のスペースを考慮して、単一の文字列を多数の小さな文字列に分割する例です。以下の例では、PHP タグ内に変数「$s1」を作成し、$s1 変数に文字列文を代入しています。次に、 print_r() 関数がその中でexplode() 関数とともに使用されます。ここで、separator1/delimeter パラメータはスペース「 」、$s1 は入力パラメータ/元の文字列であり、ここではパラメータは指定されていません。ここにはlimit/No.ofElements1パラメータがないため、文字列の分割に制限はありません。分割後、配列インデックス値の内部に小さな文字列が保存され、print_r() 関数を使用して出力されます。

構文:

リーリー

出力:

PHP 爆発()

Example #2

This is the example of implementing with limit value “0”. At first, a variable “$stra” is created and this variable is also assigned with a string value ‘car1, bus1, motorbike1, cycle1’. Then print_r() function is made along with the explode() function in it. In the explode() function, “,” is used as separator1 parameter, $stra variable value is used as OriginalString1 Parameter and value “0” is used as No.ofElements1 Parameter. It is mentioned in the parameters description that if the No.ofElements1/Limit value is mentioned as 0 then the whole original string is considered as one single string array element. This will be printed as shown in the output. Then print function is used to print line break.

Syntax:

ログイン後にコピー

Output:

PHP 爆発()

Example #3

This is the example of implementing the string splitting with the help of the positive integer as No.ofElements1/limit Parameter. Here at first, a string variable called “$strab” is created with the string value ‘car1, bus1, motorbike1, cycle1’. Then print_r() function is used along with the explode() function along with the three parameters. Here “,” is the Separator1 parameter, “$strab” is the original string element which is nothing but OriginalString1 parameter, “2” is the No.ofElements1/limit Parameter. According to the parameters description, if the positive integer value is passed then n-1 array indexes values will splitted and stored and for N-1 index value, remaining whole string will be printed.

Syntax:

ログイン後にコピー

Output:

PHP 爆発()

Example #4

This is the program of implementing the string splitting function by using different type of integer values for No.ofElements1/limit parameter. So that one can know what actually happens for different parameters which are acting inside of the explode() function. For the first explode() function, the whole original string is considered as only one array element. Then value “4” is used for the second explode() function. For this, n-1=3 array indexes string values will be printed but for n-1 array index the whole remaining string will be printed. Then for the third explode() function, negative integer value(-N) is used. So at N array index values the string will be trimmed and will be printed starting from the 0 index value.

Syntax:

ログイン後にコピー

Output:

PHP 爆発()

以上がPHP 爆発()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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