I have the following code
<?php
$str="$str="115(JOHN DOE.) 900,000.00 SALARY. 45 AGE. ";
$str=preg_replace("/[a-zA-Z(),]/", "", $str);
echo $str=preg_replace('!\s+!', ',', $str);
?>
This output
115,.,900000.00.,45.,
but this is not exactly what i need, i need to consider period which is inside 900,000.00 and ignore that of JOHN DOE. , SALARY.,AGE.
How can i do that to get
115,900000.00,45
I.e i need regular expression to consider periods after numeric character only
Without trying to improve your code...this should do it:
$str="115(JOHN DOE.) 900,000.00(SALARY.) 45(AGE.)";
$str=preg_replace("/[a-zA-Z]+\.+/", "", $str);
$str=preg_replace("/[a-zA-Z(),]/", "", $str);
echo $str=preg_replace('!\s+!', ',', $str);
I made it in three steps:
$a = "115(JOHN DOE.) 900,000.00(SALARY.)";
$a = preg_replace("/\([A-Z ]*\.\)/", "", $a); // 115 900,000.00
$b = preg_replace("/,/", "", $a); // 115 900000.00
$c = preg_replace("/\s+/", ",", $b); // 115,900000.00
You can change this line:
$a = preg_replace("/\([A-Z ]*\.\)/", "", $a);
with this:
$a = preg_replace("/[A-Z ]*\./", "", $a);
to remove data without parentheses.
$str = '115(JOHN DOE.) 900,000.00 SALARY. 45 AGE. ';
preg_match_all("/\d+(?:(?:[,.]\d+)+)?/", $str, $match);
$match = array_map(function($val) { return str_replace(",", "", $val); }, $match[0]);
echo implode(",", $match);
Output:
115,900000.00,45