我正在尝试解析和清理带有引号值的动态 CSV 字符串 (
$line
)。
echo "<pre>", print_r($line, 1), "</pre>";
字符串的格式如下:
"AARON, ELVIA J",WATER RATE TAKER,WATER MGMNT,$81000.00,$73862.00
我需要更改字符串以删除引用值中的逗号、空格和引号,使其看起来像这样:
AARONELVIAJ,WATER RATE TAKER,WATER MGMNT,$81000.00,$73862.00
我做了什么:
$re1 = '(")'; # Any Single Character 1
$re2 = '((?:[a-z][a-z]+))'; # Word 1
$re3 = '(,)'; # Any Single Character 2
$re4 = '(\\s+)'; # White Space 1
$re5 = '((?:[a-z][a-z]+))'; # Word 2
$re6 = '(\\s+)'; # White Space 2
$re7 = '.*?'; # Non-greedy match on filler
$re8 = '(")';
$reg1 = "/" . $re1 . $re2 . "/";
$reg2 = "/" . $re3 . $re4 . "/";
$reg3 = "/" . $re5 . $re6 . $re7 . $re8 . "/";
$line = preg_replace("/" . '($reg1)$reg2($reg3)' . "/", "$1$2", $line); //this is also generating the same array "AARON, ELVIA J",WATER RATE TAKER,WATER MGMNT,$81000.00,$73862.00
echo "<pre>", print_r($line, 1), "</pre>";
$pattern = "/" . $re1 . $re2 . $re3 . $re4 . $re5 . $re6 . $re7 . $re8 . "/";
$replacement = "/" . $re2 . $re5 . $re7 . "/";
$values = preg_replace($pattern, $replacement, $line);
$values = explode(',',$line);
echo "<br>";
$values = preg_replace('/[^A-Za-z0-9\-]/', '', $values);
$values = implode(',',$values);
echo "<pre>", print_r($values), "</pre>";
echo "<pre>", print_r($values, 1), "</pre>";
我得到的是这样的:
AARON,ELVIAJ,WATERRATETAKER,WATERMGMNT,8100000,73862001
我需要删除引号括起来的值中的内部逗号。
试试这个
$line = '"AARON, ELVIA J",WATER RATE TAKER,WATER MGMNT,$81000.00,$73862.00';
$pieces = explode('"', $line);
$result = '';
foreach ($pieces as $value) {
if(substr( $value, 0, 1 ) === "," || substr($value, -1) == ','){
$result .= $value;
}else{
$value = str_replace(',', '', $value);
$result .= str_replace(' ', '', $value);
}
}
echo $result;
str_getcsv()函数将为您完美完成此操作。
$result = str_getcsv($line);
给予:
Array
(
[0] => AARON, ELVIA J
[1] => WATER RATE TAKER
[2] => WATER MGMNT
[3] => $81000.00
[4] => $73862.00
)
如果您确实想删除名称中的空格和逗号,那么您可以添加:
$result[0] = preg_replace('/\s+|,/', '', $result[0]);