php - system command parameter output show results in one line using IE - php

i use this script to execute a shell from IE , while in the cli its been outputed correctly in the browser i see the results in one big line
<?php
$a = $_POST['a'];
$i=$_POST['i'];
$output = system("./xx.sh $i $a");
echo wordwrap($output,180,"<br />\n");
?>

Use this instead of wordwrap:
echo nl2br($output);
transforms line ending characters (\r\n) to <br />s
or combine:
echo wordwrap(nl2br($output), 180, "<br />\n");
or use <pre> for preformatted code:
echo "<pre>" . wordwrap($output, 180) . "</pre>";

Related

How to literally display echo contents in php

I'm looking to literally display the contents of echo or print instead of PHP processing the HTML contained within.
For example, if I have the following:
echo("<a href='$file'>$file</a> <br />\n"); the PHP parser will literally display all my files within a directory as links. What if I wanted to literally output the HTML tags without executing them so that would be displayed as plain text?
Thanks.
Instead of echo, use php function htmlentities inside a html code tag:
echo "<code>";
echo htmlentities("<a href='$file'>$file</a> <br />\n");
echo "</code>";
'<' = <
'>' = >
echo ("<a href='$file'>$file</a> <br />\n");
or...
echo htmlspecialchars("<a href='$file'>$file</a> <br />\n");

nl2br() deletes content between <br />

i'm getting a string from an mssql -database to my .php-page.
For good look, I want to replace the newlines with <br />
so I tried the following (one at a time):
echo nl2br($data);
echo str_replace(chr(10), "<br />",str_replace(chr(13), "<br />", $data))
echo str_replace("\n", "<br />",str_replace("\r", "<br />", $data))
The HTML-source code looks all right:
blablalba<br />sdsddsfdfs<br />fds<br />dfsdfs<br />fdsdsf<br />:_k,ölmjlö<br />öä.löälöä#<br />
But the result on the HTML is empty, and Chrome developer-tools is displaying following:
<br><br><br><br><br><br><br>
What am I missing?
echo $data is giving me the right result but without <br />'s
blablalba sdsddsfdfs fds dfsdfs fdsdsf :_k,ölmjlö öä.löälöä#
Regards
Use Wordwrap...
Wrap a string into new lines when it reaches a specific length:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>
Result:
An example of a
long word is:
Supercalifragulistic

How can I print a php file (source) in the browser?

How can I print a php file (source) in the browser without the php tags at the start and at the end?
So if my php file looks like this:
<?php
if( true ){
echo 'hello world';
}
?>
And in another file I want to load that file and echo the if statement so from the line 2 to 4, but not loosing the tabbing.
I have tried: fgets() and file_get_content() functions, butI can only echo an non-tabbed data.
Is there a method for this problem? or I have to write code for tabbing the source?
This should work for you:
(Here I get all lines into an array with file(). Then I cut out the first and the last line with array_slice(). After this I simply loop through the array an print it)
<?php
$lines = file("file.php");
$lines = array_slice($lines, 1, count($lines)-2);
foreach($lines as $line)
echo str_replace(" ", " ", $line) . "<br>";
?>
output:
if( true ){
echo 'hello world';
}

Can't insert newlines

I have this code:
<?php
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
echo "\n";
echo strftime('%c');
echo "\n";
echo date_default_timezone_get();
echo "\n";
?>
All three outputs should be on three separate lines, but they are all on same. What am I doing wrong?
If you want to see output on newlines in the browser use the html line break <br> instead of \n. Browsers collapse white spaces (\n is a white space) into a single ' '.
Browser interprets the output as html by default. If you want "see" the real output add this at the begin of file.
header("Content-type: text/plain");
or use a <br /> instead of simple new line
Just add The pre tag - Pre-formatted text <pre>
$ip = $_SERVER["REMOTE_ADDR"];
echo "<pre>";
echo $ip;
echo "\n";
echo strftime('%c');
echo "\n";
echo date_default_timezone_get();
echo "\n";
echo "</pre>";
php-output in the browser is formatted as Html. In Html you need the tag <br> to start a new line ... :-)
echo "<br>";
If you're viewing the page as HTML, then everything would be on one line. You need to add line breaks.
echo $ip . "<br />\n";
If you plan on viewing the output in HTML, you need to use HTML line-breaks.
<?php
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip."<br>";
echo strftime('%c')."<br>";
echo date_default_timezone_get()."<br>";
?>

New Line in PHP not working

Why Is my line break not working?
for($n=1; $n<=100; $n++)
{
echo $n '\n';
}
?>
You have syntax error there, it should be $n . '\n'
You are using ' single quote to quote the new line (\n), thus it's being interpreted as literal \ and n, change your code to: $n . "\n" to make it outputs as newline
Final code:
for($n = 1; $n <= 100; $n++)
{
echo $n . "\n"; // or "$n\n" (thanks #ring0 for pointing that out)
}
new lines are ignored in HTML. Use <br /> instead:
for($n=1; $n<=100; $n++)
{
echo $n . '<br />';
}
Use the dot (.) operator for string concatenation.
echo $n . "\n";
It needs to be in doule quotes:
Echo $n . "\n";
First thing your concatination is wrong. It should be
echo $n ."\n";
Next thing, if you are using it to output in browser, you should use <br />
echo $n."<br />";
If you are writing it to files or console and you want to be platform independent, use PHP_EOL
echo $n.PHP_EOL
Well, also remember that Newline characters are totally ignored in HTML (otherwise you'd have to do markup all on one line!)
If you're looking to get that effect, I'd recommend wrapping your output in nl2br, which converts your newlines into HTML breaks "" so that they display properly.
nl2br($n . "\n");
or just
echo $n . "<br>";

Categories