为了账号安全,请及时绑定邮箱和手机立即绑定

使用 php 解析 aptitude 搜索

使用 php 解析 aptitude 搜索

PHP
慕丝7291255 2023-10-15 15:30:42
我正在使用 PHP 和 jquery 构建一个 UI,它允许某人输入字符串并进行搜索apt search我想要做的是分离(使用 php)packaga name,就在包和架构的行the architecture之后/和正下方,这样我就可以将此数据推送到 JSON 集合中并将其返回给 jQuery。description of the package我正在执行以下命令:$data=shell_exec("sudo apt search $searchString")我可以使用爆炸获取包名称和架构,/但不知道如何获取下面的行以下是此搜索的示例输出php:wwwconfig-common/bionic 0.3.0 all  Debian web auto configurationxine-ui/bionic 0.99.9-1.3 amd64  the xine video player, user interfacexjed/bionic 1:0.99.19-7 amd64  editor for programmers (x11 version)xmlsysd/bionic 2.6.0-0ubuntu4 amd64  wulfware daemon to extract data from cluster nodesyasat/bionic 848-1ubuntu1 all  simple stupid audit toolyhsm-validation-server/bionic 1.2.0-1 all  Validation server using YubiHSMyrmcds/bionic 1.1.8-1.1 amd64  memcached compatible KVS with master/slave replicationyubikey-server-c/bionic 0.5-1build3 amd64  Yubikey validation serveryubikey-val/bionic 2.38-2 all  One-Time Password (OTP) validation server for YubiKey tokenszabbix-frontend-php/bionic 1:3.0.12+dfsg-1 all  network monitoring solution - PHP front-endzendframework/bionic 1.12.20+dfsg-1ubuntu1 all  powerful PHP frameworkzendframework-bin/bionic 1.12.20+dfsg-1ubuntu1 all  binary scripts for zendframework感谢您的帮助!更新:我可以使用爆炸“/”获取包名称和体系结构,但我不知道如何获取下面的行。我想我能找到一个漂亮的解决方案
查看完整描述

1 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

这里有几种方法可以做到这一点。


一种是使用 array_map 进行爆炸(您可以使用普通的 foreach 代替),另一种方法是使用正则表达式。


<?php

$str = 'wwwconfig-common/bionic 0.3.0 all

  Debian web auto configuration


...';

  

  

$packages = array_map(function($item) {

    

    $item = array_map('trim', explode("\n", trim($item)));

    $line = explode(" ", $item[0]);

    $line[0] = explode("/", $item[0]);

    

    return [

        'package' => $line[0][0],

        'distro' => $line[0][1],

        'version' => $line[1],  

        'arch' => $line[2], 

        'desc' => $item[1]

    ];

}, explode("\n\r", $str));

或者使用正则表达式,


preg_match_all('#(?<package>.*?)/(?<distro>.*?) (?<version>.*?) (?<arch>.*?)\n  (?<desc>.*?)\n\r#', $str, $packages);

$result = [];

foreach ($packages['package'] as $key => $value) {

    $result[] =  [

        'package' => $packages['package'][$key],

        'distro' =>  $packages['distro'][$key],

        'version' =>  $packages['version'][$key],

        'arch' =>  $packages['arch'][$key],

        'desc' =>  $packages['desc'][$key]

    ];

}


print_r($result);

两者产生相同的结果:


Array

(

    [0] => Array

        (

            [package] => wwwconfig-common

            [distro] => bionic

            [version] => 0.3.0

            [arch] => all

            [desc] => Debian web auto configuration

        )


    [1] => Array

        (

            [package] => xine-ui

            [distro] => bionic

            [version] => 0.99.9-1.3

            [arch] => amd64

            [desc] => the xine video player, user interface

        )


    [2] => Array

        (

            [package] => xjed

            [distro] => bionic

            [version] => 1:0.99.19-7

            [arch] => amd64

            [desc] => editor for programmers (x11 version)

        )


    [3] => Array

        (

            [package] => xmlsysd

            [distro] => bionic

            [version] => 2.6.0-0ubuntu4

            [arch] => amd64

            [desc] => wulfware daemon to extract data from cluster nodes

        )


    [4] => Array

        (

            [package] => yasat

            [distro] => bionic

            [version] => 848-1ubuntu1

            [arch] => all

            [desc] => simple stupid audit tool

        )


    [5] => Array

        (

            [package] => yhsm-validation-server

            [distro] => bionic

            [version] => 1.2.0-1

            [arch] => all

            [desc] => Validation server using YubiHSM

        )


    [6] => Array

        (

            [package] => yrmcds

            [distro] => bionic

            [version] => 1.1.8-1.1

            [arch] => amd64

            [desc] => memcached compatible KVS with master/slave replication

        )


    [7] => Array

        (

            [package] => yubikey-server-c

            [distro] => bionic

            [version] => 0.5-1build3

            [arch] => amd64

            [desc] => Yubikey validation server

        )


    [8] => Array

        (

            [package] => yubikey-val

            [distro] => bionic

            [version] => 2.38-2

            [arch] => all

            [desc] => One-Time Password (OTP) validation server for YubiKey tokens

        )


    [9] => Array

        (

            [package] => zabbix-frontend-php

            [distro] => bionic

            [version] => 1:3.0.12+dfsg-1

            [arch] => all

            [desc] => network monitoring solution - PHP front-end

        )


    [10] => Array

        (

            [package] => zendframework

            [distro] => bionic

            [version] => 1.12.20+dfsg-1ubuntu1

            [arch] => all

            [desc] => powerful PHP framework

        )


)


查看完整回答
反对 回复 2023-10-15
  • 1 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信