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

How to remove the last character of a string in php

王林
王林Original
2020-07-13 15:33:552911browse

The way PHP removes the last character from a string is through the substr() function. The substr() function can return a part of a string. Function syntax: [substr(string, start, length)], if it fails, it returns FALSE.

How to remove the last character of a string in php

php Deleting the last character of the string can be achieved through the substr() function and sublen() function.

(Recommended tutorial: php tutorial)

Related function introduction:

substr() function:

substr () function returns a portion of a string.

Syntax:

substr(string,start,length)

Return value:

Returns the extracted part of the string, returns FALSE if it fails, or returns an empty string.

Code implementation:

<?php
$str = "1,2,3,4,5,6,"; 
$newstr = substr($str,0,strlen($str)-1); 
echo $newstr; //echo 1,2,3,4,5,6
?>

The above is the detailed content of How to remove the last character 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