php regex用于多行,带有制表符和csv字符串的大写

时间:2015-09-12 22:18:12

标签: php regex file csv

我有几个格式为

的文本文件
CATEGORYA[can be multiple words but all caps] 
[tab]Item11[multiple upper/lower case words with spaces&numbers],$3.99
[tab]Item12,$7.49[the prices sometimes don't have the $]
etc.
[new line]
CATEGORYB[can be multiple words but all caps] 
[tab]Item21,$3.99
[tab]Item22,$7.49
etc.

我想将其转换为格式为

的csv文件
 CATEGORYA,Item11,$3.99
 CATEGORYA,Item12,$7.49
 etc.
 CATEGORYB,Item21,$3.99
 CATEGORYB,Item22,$7.49
 etc.

这是我用

开头的代码
//import file
$file = file_get_contents('./20051019.txt', true);
//split each category into an array
$catarray = preg_split("[regex of somesort]", $file);
//get number array elements
$numcats=count($catarray)
for ($x = 0; $x < $numcats; $x++)
{
//split the category from the elements
//loop through the elements replacing the tab with the category and a comma
//add element to a string
}
//write string out to a file

有人可以帮助正则表达式或知道更好的方法吗?

1 个答案:

答案 0 :(得分:0)

//import file
$file = file_get_contents('./20051019.txt', true);
//split each category into an array
$catarray = preg_split("/(\n\n|\r\r|\r\n\r\n)/m", $file);
//get number array elements
$numcats=count($catarray)
//output string
$csvstring="";
for ($x = 0; $x < $numcats; $x++)
{
    $curline=$catarray[$x];
    $elements= preg_split("/[\n\t]/", $curline);
    $numitems= count($elements);
    $cat =trim($elements[0]);
    for ($y = 1; $y < $numitems; $y++)
    {
      $csvstring=$csvstring.$cat.",".trim($elements[$y])."\n";
    }
}
//write string out to a file