简体   繁体   中英

How to print a PHP array contents with HTML link?

I cannot seem to find a solution to my specific need. I have the following:

<?php
$hostname = shell_exec('hostname');
$manufacturer = `dmidecode -s system-manufacturer | tail -1`;
$model = `dmidecode -s system-product-name | tail -1`;
$product = `dmidecode | grep SKU | cut -d: -f2`;
$serialno = `dmidecode -s system-serial-number | tail -1`;
$iloipaddr = `ipmitool lan print | grep 'IP Address  ' | cut -d\: -f2 | sed 's/ //'`;
$sftwrlist = `rpm -qa | sort`;
?>

<p><b><u><big><?php echo $hostname;?></big></u></b><br/ >
<b>Manufacturer: </b><?php echo $manufacturer;?><br/ >
<b>Model: </b><?php echo $model;?><br/ >
<b>Serial Number: </b><?php echo $serialno;?><br/ >
<b>Product Number:</b><?php echo $product;?><br/ ><p/ >
<p><a href="http://<?php echo$iloipaddr;?>">ILO link: You must be on the same private subnet for this link to work.</a><p/ >
<b><u>Installed software</u></b>
<pre><?php echo print_r($sftwrlist);?></pre>

When you point your browser at the machine you get some basic info about it, a link to its iLO and a nicely formatted list of installed software. What I'd rather have as the last line of the code is to create a link you click that displays the $sftwrlist array one package per line. I've tried various things with href but i'm not sure href is the appropriate method. Thanks in advance.

If you want to apply a link to each of the items returned from rpm -qa | sort rpm -qa | sort , explode() into an array and then apply your link to the array, you could do this on output or before like below.

<?php
$sftwrlist = `rpm -qa | sort`;

$array = explode(PHP_EOL, $sftwrlist);

$rpmlink = 'http://www.rpm-find.net/linux/rpm2html/search.php?query=';

array_walk($array, function(&$value, $key) use ($rpmlink) {
    $value = sprintf('<a href="%s%s">%s</a>', $rpmlink, $value, $value);
});

print_r($array);

https://3v4l.org/lKTm6

Result:

Array
(
    [0] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=filesystem-2.4.0-1">filesystem-2.4.0-1</a>
    [1] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=comps-extras-11.1-1.1">comps-extras-11.1-1.1</a>
    [2] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=gnome-mime-data-2.4.2-3.1">gnome-mime-data-2.4.2-3.1</a>
    [3] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=glibc-2.5-12">glibc-2.5-12</a>
    [4] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=atk-1.12.2-1.fc6">atk-1.12.2-1.fc6</a>
    [5] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=libICE-1.0.1-2.1">libICE-1.0.1-2.1</a>
    [6] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=db4-4.3.29-9.fc6">db4-4.3.29-9.fc6</a>
    [7] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=elfutils-libelf-0.125-3.el5">elfutils-libelf-0.125-3.el5</a>
    [8] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=ncurses-5.5-24.20060715">ncurses-5.5-24.20060715</a>
    [9] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=libsepol-1.15.2-1.el5">libsepol-1.15.2-1.el5</a>
    [10] => <a href="http://www.rpm-find.net/linux/rpm2html/search.php?query=libcap-1.10-26">libcap-1.10-26</a>
)

Alternatively, you could even parse the name and the version, for better display.

<?php
$array = explode(PHP_EOL, $sftwrlist);

array_walk($array, function(&$value, $key) {
    preg_match('/-\d/', $value, $match, PREG_OFFSET_CAPTURE);

    $offset = !empty($match) ? $match[0][1] : strlen($value);

    $value = [
        'name' => substr($value, 0, $offset), 
        'version' => trim(substr($value, $offset), '-')
    ];
});

print_r($array);

https://3v4l.org/HVKfE

Result:

Array
(
    [0] => Array
        (
            [name] => filesystem
            [version] => 2.4.0-1
        )

    [1] => Array
        (
            [name] => comps-extras
            [version] => 11.1-1.1
        )

    [2] => Array
        (
            [name] => gnome-mime-data
            [version] => 2.4.2-3.1
        )

    [3] => Array
        (
            [name] => glibc
            [version] => 2.5-12
        )

    [4] => Array
        (
            [name] => atk
            [version] => 1.12.2-1.fc6
        )

    [5] => Array
        (
            [name] => libICE
            [version] => 1.0.1-2.1
        )

    [6] => Array
        (
            [name] => db4
            [version] => 4.3.29-9.fc6
        )

    [7] => Array
        (
            [name] => elfutils-libelf
            [version] => 0.125-3.el5
        )

    [8] => Array
        (
            [name] => ncurses
            [version] => 5.5-24.20060715
        )

    [9] => Array
        (
            [name] => libsepol
            [version] => 1.15.2-1.el5
        )

    [10] => Array
        (
            [name] => libcap
            [version] => 1.10-26
        )
)

Thanks for all the responses. While I waited to see what everyone would suggest I did manage to come up with a method. Though it did require another php script rather than the entire code living in a single script. The new script:

<?php
$sftwrlist = `rpm -qa | sort`;
?>

<b><u>Installed software</u></b>
<pre><?php echo print_r($sftwrlist);?></pre>

And in the original script:

<p><a href="sftwr.php">Installed Software</a><p/ >

So at least I can add this to my new bag of tricks but I'll now turn my attention to the suggestions here and see what else I can learn. Any other suggestions a surely welcome while I find my way around.

I usually use:

<pre><?=json_encode($sftwrlist, JSON_PRETTY_PRINT);?></pre>

Outputs the array in JSON format, which is human readable.

UPDATE : Hadn't actually answered your question.

You can instead of an anchor tag, use some jquery to open a new window with the content. That way you don't need to mess around with redirects and routes.

function newWindow() {
  var w = window.open();
  var html = $("pre").html();

    $(w.document.body).html(html);
}

$('a').click(function(e){
    e.preventDefault();
    newWindow();
})

PS $sftwrlist isn't being shell_exec'd ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM