PHP rtrim function

 The PHP rtrim function can be used to delete the blank characters at the end of the string. In addition, PHP 4.1.0 version adds the function of the $charlist parameter, so that the rtrim function can also be removed by setting the parameter $charlist For other characters (characters), the function corresponding to rtrim is the ltrim function, which is used to clear the blank characters at the beginning of the string. If you want to clear other characters other than the ending blank, you can refer to " PHP Remove Strings "Last Character ", let’s take a look at the syntax and examples of the PHP rtrim function in this topic.


Basic syntax of PHP rtrim function
string rtrim (string $str [, string $charlist])
The $charlist in the basic syntax is a new function added after PHP 4.1.0. You can specify the list of characters you want to delete, and list all the characters you want to delete. If the $charlist parameter is not used, the rtrim function is preset "" ", \t, \n, \r, \0, \x0B" and other characters will be deleted.

PHP rtrim function example
<?php
 $text_str = "Welcome to Jason SEO ";

 $new_str_1 = rtrim($text_str);
 var_dump($new_str_1);

 echo'<br>';

 $new_str_2 = rtrim($text_str,'.');
 var_dump( $new_str_2);
?>
The above example output
string(18) "Welcome to  Jason SEO."
string(17) "Welcome to  Jason SEO"
At the beginning of the example, a string $text_str is prepared. There is a blank character at the end of the string. We delete the blank character at the end of the string through the rtrim function. After using the rtrim function for the first time, the result is stored in $ In new_str_1, after using the rtrim function for the second time, the result is stored in $new_str_2. The difference between the two times is that the second parameter of the rtrim function is used for the second time, so the last punctuation in the string is also removed. Up.

The rtrim function is very useful in dealing with general whitespace, but if you encounter full-width whitespace characters, the default mode of the rtrim function cannot remove the full-width whitespace, but it does not matter. You can use the second method in the example. That is, the full-width blank is treated as a special character and can be removed with the technique of the $charlist parameter.

Post a Comment

0 Comments