Nidal
Nidal

Reputation: 1893

Preserve whitespaces when echoing linux commandline output

when we type nslookup command on linux:

[root@vmcentos ~]# nslookup 8.8.8.8

the answer will be formatted like this:

Server:     192.168.1.1
Address:    192.168.1.1#53

Non-authoritative answer:
8.8.8.8.in-addr.arpa    name = google-public-dns-a.google.com.

Authoritative answers can be found from:
8.in-addr.arpa  nameserver = ns1.level3.net.
8.in-addr.arpa  nameserver = ns2.level3.net.
ns1.level3.net  internet address = 209.244.0.1
ns2.level3.net  internet address = 209.244.0.2

but when trying this in apache:

<?php
$var1 = `nslookup 8.8.8.8`;
echo $var1;
?>

or even:

 <?php
    $var1 = `nslookup 8.8.8.8`;
    print$var1;
 ?>

In the Browser the page shown like this:

Server: 192.168.1.1 Address: 192.168.1.1#53 Non-authoritative answer: 8.8.8.8.in-addr.arpa name = google-public-dns-a.google.com. Authoritative answers can be found from: 8.in-addr.arpa nameserver = ns2.level3.net. 8.in-addr.arpa nameserver = ns1.level3.net. ns1.level3.net internet address = 209.244.0.1 ns2.level3.net internet address = 209.244.0.2

so How to print the output of the command in the browser like the output in Linux console? (I want it the same format with the blank lines and newline (\n) )

Upvotes: 1

Views: 79

Answers (1)

Till Helge
Till Helge

Reputation: 9311

I guess the most straight-forward approach would be to surround the output with <pre> tags, so that the browser will render the text exactly as it is in the sourcecode of the page:

printf('<pre>%s</pre>', $var1);

Another solution could be to use nl2br(), which would at least preserve the line-breaks, but multiple spaces and tabs are still rendered differently by the browser.

Upvotes: 2

Related Questions