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
Release: 2023-03-05 11:54:02
Original
3775 people have browsed it

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

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

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!

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