我有一个字符串
$str = '1,2,3,4,5,"6,000",7,8,9';
我怎样才能清理它:
'1,2,3,4,5,6000,7,8,9'
我有一个字符串
$str = '1,2,3,4,5,"6,000",7,8,9';
我怎样才能清理它:
'1,2,3,4,5,6000,7,8,9'
$items = str_getcsv($str);
$items = array_map (
function ($item) { str_replace(',', '', $item); },
$items;
);
现在,如果您愿意,可以再次将它们合并在一起
$str = implode(',', $items);
尝试这样的事情(对于 PHP < 5.3):
$dst = array();
$str = '1,2,3,4,5,"6,000",7,8,9';
$state = false;
$buffer = '';
for ($i = 0, $lim = strlen($str); $i < $lim; $i += 1) {
$char = $str[$i];
switch ($char) {
case ',':
if (!$state) {
$dst[] = $buffer;
$buffer = '';
}
break;
case '"':
$state = !$state;
break;
default:
$buffer .= $char;
}
}
$dst[] = $buffer;
print_r($dst);
用这个:
$str = '1,2,3,4,5,"6,000",7,8,9';
$pattern='/"(\d+),(\d+)"/';
$replacement='${1}$2';
echo preg_replace($pattern, $replacement, $str );
谢谢:-)
<?php
$str = '1,2,3,4,5,"6,000",7,8,9';
echo $str;
echo str_replace('"','',$str);
?>