Home >Backend Development >PHP Problem >How to remove the first character on the right in php

How to remove the first character on the right in php

青灯夜游
青灯夜游Original
2022-02-23 19:22:032607browse

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)".

How to remove the first character on the right in php

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,"!");
?>

How to remove the first character on the right in php

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);
?>

How to remove the first character on the right in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn