使用 php 解析 aptitude 搜索

我正在使用 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 configuration


xine-ui/bionic 0.99.9-1.3 amd64

  the xine video player, user interface


xjed/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 nodes


yasat/bionic 848-1ubuntu1 all

  simple stupid audit tool


yhsm-validation-server/bionic 1.2.0-1 all

  Validation server using YubiHSM


yrmcds/bionic 1.1.8-1.1 amd64

  memcached compatible KVS with master/slave replication


yubikey-server-c/bionic 0.5-1build3 amd64

  Yubikey validation server


yubikey-val/bionic 2.38-2 all

  One-Time Password (OTP) validation server for YubiKey tokens


zabbix-frontend-php/bionic 1:3.0.12+dfsg-1 all

  network monitoring solution - PHP front-end


zendframework/bionic 1.12.20+dfsg-1ubuntu1 all

  powerful PHP framework


zendframework-bin/bionic 1.12.20+dfsg-1ubuntu1 all

  binary scripts for zendframework

感谢您的帮助!


更新:


我可以使用爆炸“/”获取包名称和体系结构,但我不知道如何获取下面的行。我想我能找到一个漂亮的解决方案


慕丝7291255
浏览 94回答 1
1回答

慕哥6287543

这里有几种方法可以做到这一点。一种是使用 array_map 进行爆炸(您可以使用普通的 foreach 代替),另一种方法是使用正则表达式。<?php$str = 'wwwconfig-common/bionic 0.3.0 all&nbsp; Debian web auto configuration...';&nbsp;&nbsp;&nbsp;&nbsp;$packages = array_map(function($item) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $item = array_map('trim', explode("\n", trim($item)));&nbsp; &nbsp; $line = explode(" ", $item[0]);&nbsp; &nbsp; $line[0] = explode("/", $item[0]);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; 'package' => $line[0][0],&nbsp; &nbsp; &nbsp; &nbsp; 'distro' => $line[0][1],&nbsp; &nbsp; &nbsp; &nbsp; 'version' => $line[1],&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'arch' => $line[2],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'desc' => $item[1]&nbsp; &nbsp; ];}, explode("\n\r", $str));或者使用正则表达式,preg_match_all('#(?<package>.*?)/(?<distro>.*?) (?<version>.*?) (?<arch>.*?)\n&nbsp; (?<desc>.*?)\n\r#', $str, $packages);$result = [];foreach ($packages['package'] as $key => $value) {&nbsp; &nbsp; $result[] =&nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'package' => $packages['package'][$key],&nbsp; &nbsp; &nbsp; &nbsp; 'distro' =>&nbsp; $packages['distro'][$key],&nbsp; &nbsp; &nbsp; &nbsp; 'version' =>&nbsp; $packages['version'][$key],&nbsp; &nbsp; &nbsp; &nbsp; 'arch' =>&nbsp; $packages['arch'][$key],&nbsp; &nbsp; &nbsp; &nbsp; 'desc' =>&nbsp; $packages['desc'][$key]&nbsp; &nbsp; ];}print_r($result);两者产生相同的结果:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => wwwconfig-common&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 0.3.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => Debian web auto configuration&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => xine-ui&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 0.99.9-1.3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => amd64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => the xine video player, user interface&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => xjed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 1:0.99.19-7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => amd64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => editor for programmers (x11 version)&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [3] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => xmlsysd&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 2.6.0-0ubuntu4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => amd64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => wulfware daemon to extract data from cluster nodes&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [4] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => yasat&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 848-1ubuntu1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => simple stupid audit tool&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [5] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => yhsm-validation-server&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 1.2.0-1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => Validation server using YubiHSM&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [6] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => yrmcds&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 1.1.8-1.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => amd64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => memcached compatible KVS with master/slave replication&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [7] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => yubikey-server-c&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 0.5-1build3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => amd64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => Yubikey validation server&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [8] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => yubikey-val&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 2.38-2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => One-Time Password (OTP) validation server for YubiKey tokens&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [9] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => zabbix-frontend-php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 1:3.0.12+dfsg-1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => network monitoring solution - PHP front-end&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [10] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [package] => zendframework&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [distro] => bionic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [version] => 1.12.20+dfsg-1ubuntu1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [arch] => all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [desc] => powerful PHP framework&nbsp; &nbsp; &nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP