Output execution and pattern matching

Hi all,

I’m new to PHP and have started to venture into build my first application.

The application will be used to manage system packages but already in the early stages I’ve encountered an issue I’m finding hard to resolve.

The basic idea is to query each package individually in a folder of packages with a ‘rpm -qpi <package name>’ shell command which when run outputs the following to console:

[root@mrbuild CentOS]# rpm -qpi ntp-4.2.2p1-9.el5.centos.2.1.x86_64.rpm
Name : ntp Relocations: (not relocatable)
Version : 4.2.2p1 Vendor: CentOS
Release : 9.el5.centos.2.1 Build Date: Sat 19 Dec 2009 12:56:26 AM GMT
Install Date: (not installed) Build Host: builder10.centos.org
Group : System Environment/Daemons Source RPM: ntp-4.2.2p1-9.el5.centos.2.1.src.rpm
Size : 2594086 License: distributable
Signature : DSA/SHA1, Sat 19 Dec 2009 12:01:47 PM GMT, Key ID a8a447dce8562897
URL : http://www.ntp.org
Summary : Synchronizes system time using the Network Time Protocol (NTP).
Description :
The Network Time Protocol (NTP) is used to synchronize a computer’s
time with another reference time source. The ntp package contains
utilities and daemons that will synchronize your computer’s time to
Coordinated Universal Time (UTC) via the NTP protocol and NTP servers.
The ntp package includes ntpdate (a program for retrieving the date
and time from remote machines via a network) and ntpd (a daemon which
continuously adjusts system time).

Install the ntp package if you need tools for keeping your system’s
time synchronized via the NTP protocol.

I’d like to loop this command for each package and somehow capture the output for each package ready to be inserted in to a database.

The end result being I have a database of all package names along with the information about that package.

Any help or ideas would be much appreciated.

Thanks

Mark

Well thats a large scope for us to attack, what particular issue have you got, maybe post your code so far and we can see where we can help.

Hi Mandes,

Thanks for your reply.

I’ve investigated a bit more and have got a bit further but am once again stuck. I’m trying read in the following from a file and chop it up so I can call each section separately.

e.g.

– file contents –
Name: ntp
Version: 4.2.2p1
Vendor: CentOS
Release: 9.el5.centos.2.1
Build Date: 1261184186
Build Host: builder10.centos.org
Group: System Environment/Daemons
Source RPM: ntp-4.2.2p1-9.el5.centos.2.1.src.rpm
Size: 2594086
License: distributable
URL: http://www.ntp.org
Summary: Synchronizes system time using the Network Time Protocol (NTP).
Description: The Network Time Protocol (NTP) is used to synchronize a computer’s
time with another reference time source. The ntp package contains
utilities and daemons that will synchronize your computer’s time to
Coordinated Universal Time (UTC) via the NTP protocol and NTP servers.
– end –

and what I’d like to do is have php read this and divide it up into sections e.g. name=ntp, version=4.2.2p1 and be able to call each one independantly.

I’ve been experimenting with this:

$file = “/tmp/input”;
$contents = file_get_contents($file);
$pattern = ‘/Description:.*/’;
preg_match($pattern, $contents, $matches);
echo “{$matches[0]}”;

Which appears ok for single line entries but the description is more than one line and my code only captures the matching line. The description field will always be formatted as the last entry so in theory I’d like to capture ‘Description:’ and everything below.

Thanks for your help and time

Regards

Mark