Home >Backend Development >PHP Problem >How to remove commas before and after in php

How to remove commas before and after in php

青灯夜游
青灯夜游Original
2022-04-26 15:43:172121browse

Methods to remove commas before and after: 1. Use trim() to remove commas on both sides of the string at once, using the syntax "trim($str,",")"; 2. Use ltrim() to remove the string For the comma at the beginning, use rtrim() to remove the comma at the end. The syntax is "ltrim(rtrim($str,","),",")".

How to remove commas before and after in php

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

php remove the commas before and after Two methods

  • Use trim()

  • Use ltrim() rtrim()

1. Use the trim() function

trim() function can remove commas on both sides of the string at one time

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;,1,2,3,4,5,6,&#39;;
echo trim($str,",");
?>

How to remove commas before and after in php

2. Use the ltrim() rtrim() function

  • ltrim() function can remove commas at the beginning of the string

  • rtrim() function can remove the comma at the end of the string

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;,1,2,3,4,5,6,7,8,9,&#39;;
echo ltrim($str,",")."<br>";
echo rtrim($str,",")."<br>";
echo ltrim(rtrim($str,","),",");
?>

How to remove commas before and after in php

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to remove commas before and after 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