PHP remove commas from numeric strings
Not tested, but probably something like if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)
How to remove commas from numeric values in a string with other commas?
You could also use array_map with preg_replace_callback and as the pattern you could use:
"\d{1,3}(?:,\d{3})+\.\d{2}"
"\d{1,3}
Match"
followed by 1-3 digits(?:,\d{3})!
Repeat 1+ times matching a comma and 3 digits\.\d{2}"
Match a dot and 2 digits followed by"
Regex demo | Php demo
In the callback of preg_replace_callback
replace the comma with an empty string and return the match.
For example:
$atm = array(
'26' => '20/08/2099,"ATM CASH WITHDRAWAL (ON-US) ATM CASH WITHDRAWAL (ON-US) EMPEMOM, LAGA ATM 2 LOGO NG 000360585490","",20/08/2018,"5,000","","1,316.01"',
'27' => '27/08/2027,BANK CHARGE 26 SMS CHARGE AND VAT FOR 27TH JUL - 23RD AUG 2018,2803064 028,27/08/2018,109.2,"","1,206.81"'
);
$atm = array_map(function($x) {
return preg_replace_callback('/"\d{1,3}(?:,\d{3})+\.\d{2}"/', function($m) {
return str_replace(',', '', $m[0]);
}, $x);
}, $atm);
print_r($atm);
Result:
Array
(
[26] => 20/08/2099,"ATM CASH WITHDRAWAL (ON-US) ATM CASH WITHDRAWAL (ON-US) EMPEMOM, LAGA ATM 2 LOGO NG 000360585490","",20/08/2018,"5,000","","1316.01"
[27] => 27/08/2027,BANK CHARGE 26 SMS CHARGE AND VAT FOR 27TH JUL - 23RD AUG 2018,2803064 028,27/08/2018,109.2,"","1206.81"
)
Remove comma from number format without removing decimal point in PHP
You have to assign the output of number_format
to the variable:
$value=1000000.00;
$value = number_format($value,2);
echo str_replace(",", "", $value);
Output:
1000000.00
You can also simply tell number_format
not to use a thousands separator:
$value = number_format($value,2,'.','');
echo $value;
Output:
1000000.00
Demo on 3v4l.org
how to remove comma in numbers and insert to table in laravel
You just replace the marked line with following line
'cost' => (float) str_replace(',', '', $input['cost'][$i]),
How do I remove the last comma from a string using PHP?
You may use the rtrim
function. The following code will remove all trailing commas:
rtrim($my_string, ',');
The Second parameter indicates characters to be deleted.
Remove excess commas from string PHP
$string = preg_replace("/,+/", ",", $string);
basically, you use a regex looking for any bunch of commas and replace those with a single comma.
it's really a very basic regular expression. you should learn about those!
https://regex101.com/ will help with that very much.
Oh, forgot: to remove commas in front or after, use
$string = trim($string, ",");
Remove non-numeric characters (excluding periods and commas) from a string (i.e. remove all characters except numbers, commas, and periods)
You could use preg_replace to swap out all non-numeric characters and the comma and period/full stop as follows:
$testString = '12.322,11T';
echo preg_replace('/[^0-9,.]+/', '', $testString);
The pattern can also be expressed as /[^\d,.]+/
Related Topics
Warning: MySQL_Num_Rows() Expects Parameter 1 to Be Resource, Boolean Given
PHP Readfile() Adding Extra Bytes to Downloaded File
Mysqli Insert Error Incorrect Syntax
How to Ignore a Moved-Header with File_Get_Contents in PHP
Does PHP Support Conjunction and Disjunction Natively
Best Method for Converting a PHP Array to JavaScript
PHP Session Variables Not Being Maintaned
Convert an Array of Strings, Each String Has Dot Separated Values, to a Multidimensional Array
How to Make My PHP Script Run at a Certain Time Everyday
How to Determine the Extension(S) Associated with a Mime Type in PHP
Using Mod_Rewrite with Xampp and Windows 7 - 64 Bit
Switching to Prepared Statements
Redirect to 'Www' Before Ssl Requirement
Read the Longest String from an Array in PHP 5.3
How to Add a PHP Simplexmlelement to Another Simplexmlelement