How to remove commas from string in php

青灯夜游
Release: 2023-03-15 18:14:01
Original
3434 people have browsed it

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.

How to remove commas from string in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = ",1,2,3,4,5,6,";
echo trim($str,",");
?>
Copy after login

How to remove commas from string in php

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(",",&#39;&#39;, $str)
Copy after login

Example:

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

How to remove commas from string in php

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!

Related labels:
php
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