سؤال

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 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));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top