I currently have the following php :

<?php
system('/usr/bin/whois 4.2.2.2');
?>

What is the easiest way to replace the newlines with <BR>\n.

Thanks,

Recommended Answers

All 9 Replies

Member Avatar for GreenDay2001

Check out str_replace() function (see here)

Member Avatar for diafol
nl2br()

Use nl2br() as @ardav stated. It automatically replace new line character (\n) with html 'br' tag. It prototype looks like:

nl2br(string);

echo nl2br("foo\nbar");    //return foo<br />bar

Cool, though it seems to work using strings but it doesnt seem to work when I output the system function to it..

<?php
echo '<pre>';
$output = system('/usr/bin/whois 4.2.2.2');
echo nl2br($output);
echo '</pre>';
?>

Succeed ? Then, marks as solved.

My question was how do you get nl2br to work with the output of a system command ?

Use exec instead of system and specify an empty array as the 2nd variable. This will then be filled with all the lines output by the command.
http://www.php.net/manual/en/function.exec.php

$result = array();
exec('/usr/bin/whois 4.2.2.2', $result);

Now you have an array filled with all the lines, excluding trailing whitespace, of the output.
To print this with <br />'s just use

echo implode("<br />", $result);

Use exec instead of system and specify an empty array as the 2nd variable. This will then be filled with all the lines output by the command.
http://www.php.net/manual/en/function.exec.php

$result = array();
exec('/usr/bin/whois 4.2.2.2', $result);

Now you have an array filled with all the lines, excluding trailing whitespace, of the output.
To print this with <br />'s just use

echo implode("<br />", $result);

Perfect .... Many Thanks...

Member Avatar for diafol

mark as solved if so. the link below the box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.