如果字符串中预定义有任何空格或任何其他字符,则可以使用名为 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 中的 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!");
?>
输出:
说明:
- 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:
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中文网其他相关文章!