Php 如何解析命令行字符串以使用regex获取每个参数?

Php 如何解析命令行字符串以使用regex获取每个参数?,php,regex,ssh,Php,Regex,Ssh,我正在使用PHP,试图解析从磁盘上的配置文件中获取的信息。这行代码如下所示: return $this->response = array( "host" => "example.com", "port" => "443", "user" => "root", "lport" => "22", "rport" => "2222" ); option ssh'-p 443-i/root

我正在使用PHP,试图解析从磁盘上的配置文件中获取的信息。这行代码如下所示:

    return $this->response = array(
      "host" => "example.com",
      "port" => "443",
      "user" => "root",
      "lport" => "22",
      "rport" => "2222"
    );
option ssh'-p 443-i/root/.ssh/id\u rsa-N-T-R 0.0.0.0:2222:localhost:22root@example.com

我尝试过使用regex,或者在php的
exec
下使用
awk | cut
,但没有成功

我的理想返回值如下所示:

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

如何使用RegEx从字符串中获取所需的参数?

它并不漂亮,毫无疑问可以显著改进,但它或多或少满足了要求,而不是
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
)

就像我说的,不漂亮,但是…

这种格式是固定的,它永远不会改变?这不是一个需要解析的简单字符串,解决方案取决于您是要检查它的有效性,还是只需要解析它以获得所需的字段。你应该看看,我想你应该看看,因为它看起来像是在解析命令行输入