How to remove the last few characters in php

青灯夜游
Release: 2023-03-08 20:58:01
Original
1996 people have browsed it

In PHP, you can directly use the substr() function to remove the last few characters in reverse order. The syntax format is "substr(string object, 0,-x)". The parameter x is the number of characters that need to be removed; if If successful, the string with the characters removed will be returned. If failed, FALSE will be returned, or an empty string will be returned.

How to remove the last few characters in php

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

php remove the last few characters

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$str = &#39;123,234,345,&#39;;
echo "原字符串:",$str,&#39;<br/>&#39;;
echo "去掉最后1个字符的字符串:",substr($str,0,-1),&#39;<br/>&#39;;
echo "去掉最后2个字符的字符串:",substr($str,0,-2),&#39;<br/>&#39;;
echo "去掉最后3个字符的字符串:",substr($str,0,-3),&#39;<br/>&#39;;
echo "去掉最后4个字符的字符串:",substr($str,0,-4),&#39;<br/>&#39;;
echo "去掉最后5个字符的字符串:",substr($str,0,-5);
?>
Copy after login

Output:

原字符串:123,234,345,
去掉最后1个字符的字符串:123,234,345
去掉最后2个字符的字符串:123,234,34
去掉最后3个字符的字符串:123,234,3
去掉最后4个字符的字符串:123,234,
去掉最后5个字符的字符串:123,234
Copy after login

Description:

substr() function returns a part of a string. Syntax:

substr(string,start,length)
Copy after login
Parameter Description
string Required. Specifies a part of the string to be returned.
start 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
length 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

Return value: Returns the extracted part of the string, FALSE if failed, or an empty string.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove the last few characters 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!