Home >Backend Development >PHP Problem >How to remove the last comma from a string in php

How to remove the last comma from a string in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-28 14:41:213787browse

php method to remove the last comma in a string: use the function rtrim to filter the end of the string, the code is [$trimmed = "\t\tThese are a few words :) .."$str = '13, 3';echo rtrim($str, ',')].

How to remove the last comma from a string in php

Related learning recommendations: php graphic tutorial

How to remove the last comma in a string in php:

First explain it separately,

  • trim filters both ends of the string,

  • rtrim filters the end of the string,=chop()

  • ltrim filters the beginning of the string.

To filter the keys in a string, you can only use str_replace.

For example,

PHP code

The code is as follows:

$str = '123,333,234,';
echo rtrim($str, ',');
rtrim实例代码2

The code is as follows:

<?php
$text = "\t\tThese are a few words :) ...  ";
$trimmed = rtrim($text);
// $trimmed = "\t\tThese are a few words :) ..."
$trimmed = rtrim($text, " \t.");
// $trimmed = "\t\tThese are a few words :)"
$clean = rtrim($binary, "\x00..\x1F");
// trim the ASCII control characters at the end of $binary
// (from 0 to 31 inclusive)
?>

Related learning recommendations:php programming(video)

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