该列表如下所示:
Lorem ipsum dolor sit amet,
consectetur adipisicing elit,
sed do eiusmod tempor
...
我想缩进所有这样的单词:
Lorem ipsum dolor sit amet,
consectetur adipisicing elit,
sed do eiusmod tempor
(或至少第一列)
我已经设法使用数组和str_pad
:
$lines = explode("\n", $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 .= "\n";
}
但由于文本非常大,它使用了相当多的内存。有没有其他方法可以做到这一点,而无需将文本拆分为数组?