1

我有一个字符串

$str = '1,2,3,4,5,"6,000",7,8,9';

我怎样才能清理它:

'1,2,3,4,5,6000,7,8,9'
4

4 回答 4

3

http://php.net/str-getcsv

$items = str_getcsv($str);
$items = array_map (
  function ($item) { str_replace(',', '', $item); },
  $items;
);

现在,如果您愿意,可以再次将它们合并在一起

$str = implode(',', $items);
于 2012-04-19T06:57:08.730 回答
1

尝试这样的事情(对于 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);
于 2012-04-19T07:18:03.627 回答
0

用这个:

$str = '1,2,3,4,5,"6,000",7,8,9';
$pattern='/"(\d+),(\d+)"/';
$replacement='${1}$2';

echo preg_replace($pattern, $replacement, $str );

谢谢:-)

于 2012-04-19T07:48:31.610 回答
-1
<?php
$str = '1,2,3,4,5,"6,000",7,8,9';
echo $str;
echo str_replace('"','',$str);
?>
于 2012-04-19T07:00:10.447 回答