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

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

青灯夜游
Release: 2023-03-09 22:06:02
Original
3030 people have browsed it

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!");
?>
Copy after login

Output:

Hello World!
Hello
Copy after login

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)
Copy after login
##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");
?>
Copy after login

Output:


Hello World
Hello
Copy after login

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)
Copy after login

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);
?>
Copy after login

Output:


Hello World!
Hello W
Copy after login

Instructions:

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

substr(string,start,length)
Copy after login
##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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template