请你帮我搜索数组中的十进制数或部分数字,如". "
(后面的点和空格),并用零替换它或删除点?
以下是数组的一部分:
Array ( [2] => AUD,ADF,1,06-01-2001,3.,3.9532
[3] => AUD,ADP,1,06-02-2001,99.8222,99.6682
[4] => AUD,AED,1,06-01-2001,1.8687,1.8664
[5] => AUD,AFA,1,06-01-2001,2416.57,2413.95
[6] => AUD,AFN,1,06-01-2001,2416.57,2413.95
[7] => AUD,ALL,1,06-01-2001,77.,75.9759
[8] => AUD,AMD,1,06-03-2001,NULL,NULL
[9] => AUD,ANG,1,06-01-2001,0.9056,0.9046
[10] => AUD,AOA,1,06-01-2001,3.0751,2.9961
[11] =>
请查看[7]
有77
之类的数字。或[2]
有3
。
(点后没有任何内容)。
答案 0 :(得分:4)
$array = array(
'AUD,ADF,1,06-01-2001,3.,3.9532',
'AUD,ADP,1,06-02-2001,99.8222,99.6682',
'AUD,ALL,1,06-01-2001,77.,75.9759',
'AUD,ALL,1,06-01-2001,78. ,75.9759'
);
// ----RegEx Pattern--- Replace Input
// \/ \/ \/ \/ \/
$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array);
// ^^^ ^^ ^^^^ ^^^^
// | | | |
// Match any digits [0-9] <---| | | |
// Match a point <-------| | |
// Match spaces and make it optional<-- |
// |
// Lookahead, which means that if there <--|
// is no digits after: <--|
// digit[point][0 or several spaces] <--|
// The whole thing won't be matched. <--|
// Addition: the first (\d+) is the first match group
// That's why we used $1 in replace !
// \d -> [0-9]
// \s -> space
// \. -> points have to be escaped
// (?!) -> Lookbehind (check : http://php.net/manual/en/regexp.reference.assertions.php)
// * -> Occurs 0 time or plus
// + -> occurs 1 time or plus
// The '/' at the begin and the end are delimiters
print_r($new_array);
输出:
Array
(
[0] => AUD,ADF,1,06-01-2001,3,3.9532
[1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
[2] => AUD,ALL,1,06-01-2001,77,75.9759
[3] => AUD,ALL,1,06-01-2001,78,75.9759
)
编辑:要回答以下评论中的问题:
正则表达式完全是关于常规模式。从所需的输出,我可以看到你想把所有内容放在除数字(整数和双/浮点数)之外的引号中,我们还有引号之间的日期。 所以这是一种方法:
$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array); // remove some dots & spaces
$new_array_2 = preg_replace('/([a-zA-Z]+|\d+-\d+-\d+)/', '\'$1\'', $new_array);
// Match letters <--^-^-^-^-^ ^-^-^-^-^---> Match digits-digits-digits (for the date part), I don't want to use a LOOOONG RegEx to check if it's valid ...
print_r($new_array); // First replacement
print_r($new_array_2); // Second replacement
通过使用一些PHP-Fu,还有另一种更可靠的方法:
$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array); // remove some dots & spaces
// For this code YOU NEED PHP 5.3+ since it's using anonymous functions
$new_array_2 = array_map(function($val){
$pieces = explode(',', $val);
$pieces2 = array_map(function($val2){
if(preg_match('/^\d+(\.\d+)?$/', $val2)){
return $val2;
}else{
return "'$val2'";
}
}, $pieces);
return(implode(',',$pieces2));
}, $new_array);
print_r($new_array);
print_r($new_array_2);
输出:
Array
(
[0] => AUD,ADF,1,06-01-2001,3,3.9532
[1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
[2] => AUD,ALL,1,06-01-2001,77,75.9759
[3] => AUD,ALL,1,06-01-2001,78,75.9759
)
Array
(
[0] => 'AUD','ADF',1,'06-01-2001',3,3.9532
[1] => 'AUD','ADP',1,'06-02-2001',99.8222,99.6682
[2] => 'AUD','ALL',1,'06-01-2001',77,75.9759
[3] => 'AUD','ALL',1,'06-01-2001',78,75.9759
)
答案 1 :(得分:1)
您可以使用intval()将浮点转换为整数:http://php.net/manual/en/function.intval.php 看起来您可能需要拆分值(用逗号分隔),转换它们然后再次加入它们......
答案 2 :(得分:1)
使用str_replace,您可以将完整数组作为输入传递:
$ j = 阵列( 'AUD,ADF,1,06-01-2001,3。,3.9532', 'AUD,ADP,1,06-02-2001,99.8222,99.6682','AUD,AED,1,06-01- 2001,1.8687,1.8664' , 'AUD,AFA,1,06-01-2001,2416.57,2413.95', 'AUD,AFN,1,06-01-2001,2416.57,2413.95','AUD,ALL,1, 06-01-2001,77,75.9759' , '澳元,AMD,1,06-03-2001,NULL,NULL', '澳元,ANG,1,06-01-2001,0.9056,0.9046','AUD ,AOA,1,06-01-2001,3.0751,2.9961' );
$ j = str_replace(“。,”,“,”,$ j);
的print_r($ j)的;
输出
Array
(
[0] => AUD,ADF,1,06-01-2001,3,3.9532
[1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
[2] => AUD,AED,1,06-01-2001,1.8687,1.8664
[3] => AUD,AFA,1,06-01-2001,2416.57,2413.95
[4] => AUD,AFN,1,06-01-2001,2416.57,2413.95
[5] => AUD,ALL,1,06-01-2001,77,75.9759
[6] => AUD,AMD,1,06-03-2001,NULL,NULL
[7] => AUD,ANG,1,06-01-2001,0.9056,0.9046
[8] => AUD,AOA,1,06-01-2001,3.0751,2.9961
)
答案 3 :(得分:0)
你必须使用array_walk()和preg_replace()来做到这一点。正则表达式的表达式应该类似于(。)[,\ s]