Home > Article > Backend Development > How to remove commas before and after in php
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,","),",")".
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('content-type:text/html;charset=utf-8'); $str = ',1,2,3,4,5,6,'; echo trim($str,","); ?>
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('content-type:text/html;charset=utf-8'); $str = ',1,2,3,4,5,6,7,8,9,'; echo ltrim($str,",")."<br>"; echo rtrim($str,",")."<br>"; echo ltrim(rtrim($str,","),","); ?>
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!