I currently have the following php :
<?php
system('/usr/bin/whois ');
?>
What is the easiest way to replace the newlines with <BR>\n.
Thanks,
the core issue: system() streams the command output directly to the browser and only returns the last line to $output. So nl2br($output) touches just that last line. Once you capture the full output into a string, nl2br() will do what you expect (it also handles \r\n, \n, and \r). @diafol and were right about using nl2br(); showed one way to capture. Here are two others you can drop in. (php.net)
Option A: capture with shell_exec() and escape before printing.
$target = '4.2.2.2';
$out = shell_exec('/usr/bin/whois ' . escapeshellarg($target));
echo nl2br(htmlspecialchars($out ?? '', ENT_QUOTES, 'UTF-8'), false); shell_exec() returns the complete output as a string; htmlspecialchars() prevents any accidental HTML from whois data breaking your page; nl2br(..., false) emits <br> tags. (php.net)
Option B: keep system() but capture its printed output via output buffering.
$target = '4.2.2.2';
ob_start();
system('/usr/bin/whois ' . escapeshellarg($target));
$out = ob_get_clean();
echo nl2br(htmlspecialchars($out, ENT_QUOTES, 'UTF-8'), false); Output buffering grabs what system() would have sent to the browser, so you can post-process it safely. (php.net)
Small notes:
escapeshellarg() to avoid command injection. (php.net)<pre>...</pre>, you usually do not need <br> tags at all since <pre> preserves newlines; choose one or the other.str_replace() tip works, but nl2br() is aware of multiple newline conventions and is simpler for HTML output. (php.net)Jump to Post— Member #114696Check out
str_replace()function (see here)
Jump to Post— Member #120589nl2br()
Jump to Post— ko ko 97Use 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
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 useecho implode("<br />", $result);
Perfect .... Many Thanks...
mark as solved if so. the link below the box.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.