提问人:Sultanov 提问时间:6/7/2020 更新时间:6/7/2020 访问量:39
引用单词并用逗号分隔
Quoting words and separating them with commas
问:
任务是将大量数据导入数据库。最好的选择,我考虑导入csv。但是仍然存在一个问题,即数据不是csv格式。我不得不自己写一个剧本。仅当指定一行时,脚本才能正常工作,但如果发送多个数据,则脚本将开始无法正常工作。
众所周知,有线条,任务就是把这些数据带到这种. - 有时它没有一定的大小,有时发生在一个单词中,有时可能是 5-6 个单词。
正如我所说,当我每次处理只给它一行时,脚本工作正常,即 ,它返回 .但是当我给脚本多行时,即 ,脚本返回给我的结果略有不同 - 我做错了什么?123 1234567 Some text
"123","1234567","Some text"
“Some text”
$string = '123 1234567 Some text'
"123","1234567","Some text"
$string = '123 1234567 Some text 321 7654321 Some text more'
"123","1234567","Some text 123","7654321","Some text more"
if (! function_exists("csv_prepare")) {
function csv_prepare($string) {
$exp = explode(" ",$string);
$str = '';
$k=0;
foreach ($exp as $key => $value) {
if( preg_match('/[0-9]{3}/', $value) || preg_match('/[0-9]{7}/', $value) )
{
$str .= $value . ',';
}else{
$str .= $value . ' ';
}
}
$str = explode(",",$str);
$finish = '';
foreach ($str as $key => $value) {
if($key != array_key_last($str))
{
$finish .= '"'.$value.'"'.',';
}else{
$finish .= '"'.$value.'"';
}
}
return $finish;
}
}
// END csv prepare```
答: 暂无答案
评论
explode()
explode
$exp = explode("\n",$string);
喜欢这个?function csv_prepare($string) { $string = explode("\n\r",$string); foreach($string as $key => $value) { $exp = explode(" ",$value); }