PHP 砍()

PHPz
發布: 2024-08-29 12:52:50
原創
200 人瀏覽過

如果字串中預先定義有任何空格或任何其他字符,則可以使用名為Chop() 的函數將它們從字串的右側刪除,該函數也是名為rtrim( 的函數的別名) ) PHP 中的函數。 PHP第四版及第四版之後的上述版本支援此函數,PHP中該函數的傳回值是字串,但與傳遞給函數的輸入字串相比,它是變化的字串。

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

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

文法

在PHP中宣告chop()函數的語法如下:

chop(string_name, char_list)
登入後複製

哪裡,

  • string_name:是需要修剪的字串的名稱,即必須套用chop()函數。這是語法的基本參數,而且是強制性的。此參數的資料類型為字串資料類型。
  • char_list: 它指定要從作為參數傳遞的字串中刪除的字元清單。如果在呼叫的chop()函數中沒有指定char_list參數,chop()函數將像普通空格一樣應用於以下字元列表,“也稱為空字節,t”,即也稱為製表符,n”也稱為換行符,x0B」也稱為垂直製表符,「r」也稱為回車符。這不是語法的必需參數且是可選的。此參數的資料類型為字串資料類型。

chop() 函數在 PHP 中如何運作?

Chop() 函數是 PHP 中的字串函數之一。可以使用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()函數,我們可以刪除字串右側的空格或特定字元。在我們的程式中,我們對字串應用 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,可以在快照中看到。 PHP 中的每個 echo 語句中都使用「n」來在新行中列印每行的輸出。不同類型字串上的不同字元可以執行相同的操作。

PHP Chop() 範例

以下是範例:

範例#1

PHP 程式來說明 Chop() 函數在字串上的工作原理。

代碼:

<?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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!