php remove comma from end of string code example

Example 1: php remove last character from string if comma

$string = rtrim($string, ',');

Example 2: php remove everything after character

$fullpath = 'folderName/file.ext';
$folder = substr($fullpath, 0, strpos($fullpath, '/'));
echo $folder;
// Output => folderName

Example 3: remove comma in numeric in php

$var = 18,542.00;
$var = intval(preg_replace('/[^\d.]/', '', $var));
result :- 18542;
or if you need float
$var = floatval(preg_replace('/[^\d.]/', '', $var));
result :- 18542.00;

Example 4: remove string after comma in php

preg_replace('/^([^,]*).*$/', '$1', $print);
substr($string, 0, strrpos($string.",", ","));

Tags:

Php Example