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

How to remove the first character on the right in php

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-06-19 10:47:301324browse

php method to remove the first character on the right is: 1. Create a php sample file; 2. Use the built-in function "strlen()" to get the string length and save it as $str; 3. Use "substr ()" function extracts the string starting from 0 to length -1 and saves it as $new_str; 4. Echo outputs $new_str.

How to remove the first character on the right in php

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

PHP can use the built-in functions substr() and strlen() to remove the first character on the right.

The specific steps are as follows:

  1. Use strlen() to get the length of the string

  2. Use substr() Extract the substring of the string starting from 0 to the length minus 1

Here are some sample codes:

```php
<?php
// 示例字符串
$str = "Hello World!";
// 去除右侧第一个字符
$new_str = substr($str, 0, strlen($str) - 1);
echo $new_str; // 输出:Hello World
?>
```

Also, if you know what characters you want to delete, you can Use the rtrim() function:

```php
<?php
// 示例字符串
$str = "Hello World!";
// 去除右侧的感叹号
$new_str = rtrim($str, "!");
echo $new_str; // 输出:Hello World
?>
```

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