PHP チョップ()

PHPz
リリース: 2024-08-29 12:52:50
オリジナル
199 人が閲覧しました

文字列内に事前定義された空白やその他の文字がある場合、chop() 関数と呼ばれる関数を使用して、文字列の右側からそれらを削除できます。これは、rtrim( と呼ばれる関数のエイリアスでもあります) ) PHP の関数。この関数は、PHP の 4 番目のバージョンと、4 番目のバージョン以降の上記のバージョンでサポートされており、PHP でのこの関数の戻り値は文字列ですが、関数に渡された入力文字列と比較すると変更された文字列です。

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

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

構文

PHP で Chop() 関数を宣言する構文は次のとおりです。

chop(string_name, char_list)
ログイン後にコピー

どこ、

  • string_name: トリミングが必要な文字列の名前です。つまり、chop() 関数を適用する必要があります。これは構文の必須パラメータであり、必須です。このパラメータのデータ型は文字列データ型です。
  • char_list: パラメータとして渡される文字列から削除する文字のリストを指定します。呼び出されるchop()関数でchar_listパラメータが指定されていない場合、chop()関数は、通常のスペース「Nullバイトとも呼ばれるt」と同様に、次の文字のリストに適用されます。タブとも呼ばれる「n」はラインフィードとも呼ばれ、「x0B」は垂直タブとも呼ばれ、「r」はキャリッジリターンとも呼ばれます。これは構文の必須パラメータではなく、オプションです。このパラメータのデータ型は文字列データ型です。

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

Chop() 関数は、PHP の文字列関数の 1 つです。文字列の右側にある必須の空白文字または指定された文字または文字列は、PHP の Chop() 関数を使用して削除できます。

以下のプログラムについて考えてみましょう:

コード:

<?php
//declaring the string Shobha.Shivakumar#
$string = "Shobha.Shivakumar#";
//Using chop() function to remove the character # from the right side of the string
echo (chop($string, "#")."\n");
//Using chop() function to remove the characters akumar# from the right side of the string
echo (chop($string, "akumar#")."\n");
//Using chop() function to remove the characters .Shivakumar# from the right side of the string
echo (chop($string, "ha.Shivakumar#")."\n");
?>
ログイン後にコピー

出力:

PHP チョップ()

説明:

  • 上記のプログラムでは、PHP の Chop() 関数を適用する必要がある文字列が宣言されています。上記のプログラムで宣言された文字列は Shobha.Shivakumar# です。 PHP で Chop() 関数を使用すると、文字列の右側から空白や特定の文字を削除できます。私たちのプログラムでは、文字列にチョップ関数を適用して、文字列 Shobha.Shivakumar# から # 文字を削除します。
  • PHP の echo ステートメントは、指定された文字列に Chop() 関数を適用した後に変更された文字列を出力するために使用されます。 Echo (chop($string, “#”).”n”);この操作を実行するために PHP でステートメントが使用されます。この出力は Shobha.Shivakumar であり、スナップショットで確認できます。次に、文字列に Chop 関数を適用して、文字列 Shobha.Shivakumar# から akumar# 文字を削除します。 PHP の echo ステートメントは、指定された文字列に Chop() 関数を適用した後に変更された文字列を出力するために使用されます。 echo (chop($string, “akumar#”).”n”);この操作を実行するために PHP でステートメントが使用され、その出力は Shobha.Shiv であり、スナップショットで確認できます。
  • 次に、文字列に Chop 関数を適用して、文字列 Shobha.Shivakumar# から ha.Shivakumar# 文字を削除します。 PHP の echo ステートメントは、指定された文字列に Chop() 関数を適用した後に変更された文字列を出力するために使用されます。 echo (chop($string, “Shivakumar#”).”n”);この操作を実行するために PHP でステートメントが使用されます。この出力は Shob であり、スナップショットで確認できます。 「n」は、各行の出力を新しい行に出力するために、PHP のすべての echo ステートメントで使用されます。異なる種類の文字列の異なる文字に対して同じ操作を実行できます。

PHP Chop() の例

以下に例を示します:

例 #1

文字列に対する Chop() 関数の動作を説明する PHP プログラム。

コード:

<?php
//declaring the string Shobha.Shivakumar!
$string= "Shobha.Shivakumar!";
//printing the string on which the chop() function will be applied and making sure the next statement will be printed in a new line
echo $string. "\n";
//printing the changes string after the application of the input string
echo chop($string, "r!");
?>
ログイン後にコピー

出力:

PHP チョップ()

説明:

  • In the above program, a string is declared upon which the chop() function in PHP must be applied. The string declared in the above program is Shobha.Shivakumar!. By using chop() function in PHP, we can remove the whitespaces or the specific characters from the right side of the string. In our program, we apply the chop function on the string to remove the characters r! from the string Shobha.Shivakumar!
  • An echo statement in PHP is used to print the changed string after the application of the chop() function on the given string. echo (chop($string, “r!”); statement is used in PHP to perform this operation and the output of this is Shobha.Shivakuma which can be seen in the snapshot. “\n” is used in the second echo statement in PHP to sure the next statement will be printed in a new line. The same operation can be performed on different character on different types of strings.

Example #2

PHP program to illustrate the working of chop() function on strings.

Code:

<?php
//declaring the string Hello! followed by Shobha Shivakumar in a new line, the string is followed by two new line characters
$string= "Hello! \n Shobha Shivakumar \n \n";
//the string is printed
echo $string;
//chop() function is applied on the string without specifying any parameter
echo chop($string);
//The changed string is printed
echo $string;
?>
ログイン後にコピー

Output:

PHP チョップ()

Explanation:

  • In the above program, a string is declared upon which the chop() function in PHP must be applied. The string declared in the above program is Hello! \n Shobha Shivakumar \n \n. By using chop() function in PHP, we can remove the whitespaces or the specific characters from the right side of the string. In our program, we apply the chop function on the string without specifying any parameters.
  • An echo statement in PHP is used to print the changed string after the application of the chop() function on the given string. echo $string; statement is used in PHP to perform this operation and the output of this is Hello! Followed by Shobha Shivakumar in a new line and two new line spaces after that can be seen in the snapshot. “\n” is used in the echo statement in PHP to sure the next statement will be printed in a new line. The output after the application of chop() function without specifying the parameters removes the two line characters from the right side of the string.

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

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