php - How do I parse a command line string to get each argument using regex? -


i'm working in php , trying parse information configuration file on disk. line looks this:

option ssh '-p 443 -i /root/.ssh/id_rsa -n -t -r 0.0.0.0:2222:localhost:22 root@example.com

i've tried regex or using awk|cut under php's exec i'm not having success.

my ideal return value looks this:

    return $this->response = array(       "host" => "example.com",       "port" => "443",       "user" => "root",       "lport" => "22",       "rport" => "2222"     ); 

how arguments need string regex?

it's not pretty , undoubtedly improved markedly more or less requested, other 2222 ???

$str="option ssh      '-p 443 -i /root/.ssh/id_rsa -n -t -r 0.0.0.0:2222:localhost:22 root@example.com";  $pttn='#(-p (\d{1,3}) .*:(\d+):localhost:(\d+) ((\w+)@(.*)))#'; preg_match( $pttn, $str, $matches ); $response=array( 'user'=>$matches[6], 'port'=>$matches[2], 'host'=>$matches[7], 'lport'=>$matches[4], 'rport'=>$matches[3] );   print_r( $response );  outputs: -------- array (     [user] => root     [port] => 443     [host] => example.com     [lport] => 22     [rport] => 2222 ) 

like said, not pretty but...


Comments