Removal method: 1. Use trim(), the syntax "trim($str,",")" to delete the commas on both sides of the string; 2. Use str_replace(), the syntax "str_replace(" ,",'', $str)" can replace all commas in the string with null characters to delete all commas.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php removes strings Commas
1. Use the trim() function
trim() function to remove whitespace characters or other predefined characters on both sides of the string. character.
You only need to set the second parameter of the function to ",
" to delete the commas on both sides of the string.
<?php header('content-type:text/html;charset=utf-8'); $str = ",1,2,3,4,5,6,"; echo trim($str,","); ?>
2. Use str_replace() function
In PHP, str_replace uses a new string to replace the original string The specified specific string (character); when the replacement value is set to the null character '', the specific character is deleted.
The syntax for deleting commas:
str_replace(",",'', $str)
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = ",1,2,3,4,5,6,"; $replace = ''; $search = ','; echo str_replace($search, $replace, $str); ?>
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How to remove commas from string in php. For more information, please follow other related articles on the PHP Chinese website!