开发者

Split the output in PHP

I'm trying to use either split, preg_split, or explode to parse the following data into an array so that I can easily print and modify the data:

28782188    /var/opt

When I run

print_r(preg_split('/ /', $input));

All I get开发者_StackOverflow is

Array ( [0] => 28782796 /var/opt )

Is there a way to make php split with the whitespace that I'm getting from my du calls?


I think you want

preg_split('/\s+/',$input);

To split by any white-space character - du seperates with tabs (\t) if I remember right, although don't quote me on that...

EDIT Changed regex to one that works...


<?php 
  $value = '28782188 /var/opt';
  $values = array();

  //du might separate size and directory with **multiple** space/tabs
  preg_match_all('/\w*\S[\w\/]*/', $value, $values, PREG_PATTERN_ORDER);

  print_r($values);
  // outputs: Array ( [0] => '28782188', [1] => '/var/opt' )
?>


Try print_r(explode(' ', $input)); to break input over whitespace.


Just use explode...

print_r(explode(' ', $input));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜