php remove comma from string – how to remove comma from string in php?

php remove comma from string. To remove comma or any special char, you can replace. To replace, use str_replace() in PHP. Then use trim() to remove unnecessary commas at the front or end of the string.

php remove comma from string

trim comma from variable in php

rtrim($my_user_line,',');

Remove comma from a string in PHP?

Example

<!DOCTYPE html>
<html>
<body>
<?php
$name="Infinityknow,Pakainfo";
$result = str_replace(',', '', $name);
echo "The actual value is=",$name,"<br>";
echo "After removing the comma(,)=",$result,"<br>";
?>
</body>
</html>

PHP remove commas from numeric strings

Example

$a = "1,895";
$b = str_replace( ',', '', $a );

if( is_numeric( $b ) ) {
    $a = $b;
}

How to remove the last comma from a PHP foreach loop?

Remove the comma from the last item of PHP foreach loop

<?php
  $website_list = array("pakainfo", "w3diy", "infinityknow", "w3school", "blogspot", "example");
  $num_of_items = count($website_list);
  $num_count = 0;
  foreach ($website_list as $website) {
    echo $website;
    $num_count = $num_count + 1;
    if ($num_count < $num_of_items) {
      echo ", ";
    }
  }
?>

Remove Last Comma From String Jquery

Read Also:  PHP Pagination demo – Pagination in PHP with example script

remove comma from string php

Remove an item from comma separated string using PHP

<?php
function removeItemuser_line($str, $item) {
	$parts = explode(',', $str);
	while(($i = array_search($item, $parts)) !== false) {
		unset($parts[$i]);
	}
	return implode(',, $parts);
}

Example

<?php
$myuser_line = 'pakainfo,infinityknow,w3diy,w3school';
$result_user_line = removeItemuser_line($myuser_line, "mouse");
echo "Input user_line: ".$myuser_line;
echo “Output user_line: ".$result_user_line;
?>

I hope you get an idea about php remove comma from string.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.