转换复杂的文本列表

时间:2014-05-07 18:05:41

标签: php mysql excel

我在一个简单的文本文档(80K字)中有一个大单词列表,如下所示:

needy
    needier, neediest, neediness
need
nefarious
    nefariously, nefariousness
negate
    negated, negates, negating

我想把它变成这样的东西:

needy needy
needy needier
needy neediest
needy neediness
need need
nefarious nefarious    
nefarious nefariously
nefarious nefariousness
negate negate  
negate negated
negate negates
negate negating

你怎么建议我这样做?我可以做一些php mysql,或者我可以在Excel上工作。

1 个答案:

答案 0 :(得分:1)

说明:

代码打开你的文本文件并将它们抓取到一个数组中,然后将它们分成2个块。它通过循环迭代,第二个数组元素使用逗号分解并连接到第一个元素,然后添加到新数组中。

<?php
echo "<pre>";
$arr = file('stats.txt',FILE_IGNORE_NEW_LINES); //<--- Point the filename of your text document
$arr = array_chunk($arr,2);
$new_arr = array();
foreach($arr as $k=>$arr1)
{
    $v = explode(',',$arr1[1]);
    foreach($v as $val)
    {
        $new_arr[]=$arr1[0]." ".trim($val);
    }
}
print_r($new_arr);

Demonstration

<强> OUTPUT :

Array
(
    [0] => needy needier
    [1] => needy neediest
    [2] => needy neediness
    [3] => nefarious nefariously
    [4] => nefarious nefariousness
    [5] => negate negated
    [6] => negate negates
    [7] => negate negating
)