drjun1994 2013-03-25 23:24
浏览 79
已采纳

找出列表中最长单词的长度

The list looks like:

Lorem ipsum dolor sit amet,
consectetur adipisicing elit,
sed do eiusmod tempor
...

I want to indent all words like this:

Lorem       ipsum       dolor   sit    amet,
consectetur adipisicing elit,
sed         do          eiusmod tempor

(or at least the first column)

I've managed to do this using arrays and str_pad:

$lines = explode("
", $text);
$counts = array();

foreach($lines as $line){
  $words = explode(' ', $line);
  foreach($words as $index => $word){
    if(!isset($counts[$index]) || $counts[$index] < strlen($word))
      $counts[$index] = strlen($word);
  }
}

$text = '';

foreach($lines as $line){
  $words = explode(' ', $line);
  foreach($words as $index => $word)
    $text .= str_pad($word, $counts[$index], ' ' , STR_PAD_RIGHT) . ' ';

  $text .= "
";
}

but because the text is very large it uses quite a lot of memory. Is there any other way I can do this, without splitting the text in arrays?

  • 写回答

1条回答 默认 最新

  • dqh1992 2013-03-26 05:33
    关注

    If the file is extremely large, you could read the file line by line instead of loading it all into an array at once with file().

    $fh = fopen("alice.txt", "r");
    $fh = fopen("yourfile.txt", "r");
    
    while( !feof($fh) ){
    
        $line = fgets($fh);
        $Newerline = str_replace("-", " ", $line);
    
        $textArray = str_word_count($Newerline,1); 
    
        //now go through the line word by word
        foreach ($textArray as $index => $word){
            trim($word);
            $wordLength = mb_strlen($word);
    
            //is this word the longest for this column?
            if($wordLength > $longWords[$index]['longestWordlen'] ){
                $longWords[$index]['longestWordlen'] = $wordLength;
                $longWords[$index]['longestWord'] = $word;
            }
    
        }
        //echo $line;
    }
    //rewind the file pointer to the beginning
    rewind($fh);
    
    ksort($longWords);//associative array will be out of order since longest word shows up differently
    
    //now create a new file to hold the new version
    $fileh = fopen( "newFile.txt", "c");
    
    while( !feof($fh) ){
        $line = fgets($fh);
        $Newerline = str_replace("-", " ", $line); 
    
        $textArray = str_word_count($Newerline,1); 
    
        $newLine =  "";
        //now go through the line word by word
        foreach ($textArray as $index => $word){
            //set the length of this words column
                $pad_length = $longWords[$index]['longestWordlen'];
    
                $newLine .= str_pad($word, $pad_length + 1, " ", STR_PAD_RIGHT);
    
        }
        $newLine .= PHP_EOL;
    
        //save the newLine to file
        fwrite($fileh, $newLine);
    
        //echo $newLine;
    }
    
    fclose($fileh);
    fclose($fh);
    

    This loads one line at a time and writes one line at a time. Should use less memory.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示