Home  >  Article  >  Backend Development  >  How to remove the last few characters of a string in php

How to remove the last few characters of a string in php

青灯夜游
青灯夜游Original
2021-05-25 18:07:393000browse

Removal method: 1. Use the rtrim() function, the syntax is "rtrim(string, character)"; 2. Use the chop() function, the syntax is "chop(string, character)"; 3. Use substr() function, syntax "substr(string,0,-n)", parameter n specifies the number of characters to be removed.

How to remove the last few characters of a string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: Use rtrim() function

<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>

Output:

Hello World!
Hello

Description:

rtrim() function from string Starting from the end, whitespace characters or other predefined characters are removed. The syntax is as follows:

rtrim(string,charlist)
##ParameterDescriptionRequired. Specifies the string to check. Optional. Specifies which characters are removed from a string. If this parameter is omitted, all of the following characters are removed:
string
charlist
    "\0" - NULL
  • "\t" - Tab
  • "\n" - Newline
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - space

Method 2: Use chop() function

<?php
$str = "Hello World";
echo $str . "<br>";
echo chop($str,"World");
?>

Output:


Hello World
Hello

Description:

chop() function removes blank characters or other predefined characters at the right end of the string. The syntax is as follows:

chop(string,charlist)

Parameter description is the same as the rtrim() function.

Method 3: Use substr() function

<?php
$str = "Hello World!";
echo $str . "<br>";
echo substr($str,0,-5);
?>

Output:


Hello World!
Hello W

Instructions:

substr() function returns a part of a string. The syntax is as follows:

substr(string,start,length)

##Parameterstringstartlength Note: If the start parameter is negative and length is less than or equal to start, length is 0.
Description
Required. Specifies a part of the string to be returned.
Required. Specifies where in the string to begin. Positive number - starts at the specified position in the string
  • Negative number - starts at the specified position from the end of the string
  • 0 - at the first character in the string Start at
Optional. Specifies the length of the string to be returned. The default is until the end of the string. Positive number - Returns from the position of the start parameter
  • Negative number - Returns from the end of the string

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to remove the last few characters of a string 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