Assign variables

Discussion in 'PHP' started by 3.5supersonic, Jul 30, 2009.

  1. #1
    Hi im not good in php much i want to know how to do this. please help me :)

    im executing shell command in php like this

    exec('du -s ' . $homepath . escapeshellcmd($username), $return); 
    Code (markup):
    this output something like this

    25632508 /home/user1
    Code (markup):
    so i want to assign only 25632508 to a variable like $usage
    i dont need /home/user1 part.

    How can i do it?

    Thank you.
     
    3.5supersonic, Jul 30, 2009 IP
  2. thedanielsolution

    thedanielsolution Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Try this:

    $parts = explode(' ',$return);
    $usage = $parts[0];

    Hope that helps.
     
    thedanielsolution, Jul 30, 2009 IP
  3. 3.5supersonic

    3.5supersonic Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    its not work for me if i echo $usage im getting out put as Array
     
    3.5supersonic, Jul 30, 2009 IP
  4. thedanielsolution

    thedanielsolution Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    0
    #4
    hmmm.. Odd.

    Works for me, the echo delivers the first part of the $return

    Lets try this:

    $the_txt = exec('du -s ' . $homepath . escapeshellcmd($username), $return);

    $parts = explode(' ',$the_txt);
    $usage = $parts[0];

    Again.. just a quick attempt, not tested.
     
    thedanielsolution, Jul 30, 2009 IP
  5. 3.5supersonic

    3.5supersonic Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ya now it works!!! Thanks mate.

    One small question

    if out put is like 25G /home/user1
    [lot of spaces between 25G and /home/user1 ]

    is there any way to assign this ?
     
    3.5supersonic, Jul 30, 2009 IP
  6. thedanielsolution

    thedanielsolution Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Glad that worked out for you!

    For the lots of spaces... I suppose you could spit it at the '/' -- > altho you will get more parts, its still the first part you want, so that should also work for you.

    D
     
    thedanielsolution, Jul 30, 2009 IP
  7. 3.5supersonic

    3.5supersonic Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you very much for your time
     
    3.5supersonic, Jul 30, 2009 IP