Home >Backend Development >PHP Problem >How to remove the first character on the right in php
php method to remove the first character on the right: 1. Use the rtrim() function to remove the specified character on the right side of the string, the syntax is "rtrim (string, specified character)"; 2. Use substr() and strlen() functions, the syntax is "substr(string,0,strlen(string)-1)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
How to remove the right side of php The first character
#1. Use the rtrim() function
The rtrim() function can remove the specified characters on the right side of the string.
<?php $str = "Hello World!"; echo $str . "<br>"; echo rtrim($str,"!"); ?>
2. Use the substr() and strlen() functions
The substr() function can start from the specified position of the string Intercept a certain length of characters. The intercepted characters can be called "substring" or "substring",
<?php $str = "Hello World~"; echo $str . "<br>"; echo substr($str,0,strlen($str)-1); ?>
Recommended learning: "PHP Video tutorial》
The above is the detailed content of How to remove the first character on the right in php. For more information, please follow other related articles on the PHP Chinese website!