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>";
?>
Related
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");
I have the following line in my code which displays my output in 6 characters with leading zeros.
$formatted_value = sprintf("%06d", $phpPartHrsMls);
I want to replace the leading zeros with spaces. Have tried all the examples found by searching this site and others and cannot figure it out.
Here are some I have tried:
$formatted_value = sprintf("%6s", $phpPartHrsMls);
$formatted_value = printf("[%6s]\n", $phpPartHrsMls); // right-justification with spaces
In the browser, spaces will always be collapsed.
Try:
<pre><?php echo $formatted_value; ?></pre>
And once you're satisfied with that, take a look at the CSS white-space:pre-wrap - a very useful property!
This will align the left with spaces:
$formatted_value = sprintf("%6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
This will align the right with spaces:
$formatted_value = sprintf("%-6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
If you want to print only six digits, and others to remove:
$formatted_value = sprintf("%-6.6s", $phpPartHrsMls);
echo "<pre>" . $formatted_value . "</pre>";
One more thing, the browser will generally ignore the spaces, so it's better to wrap your output in <pre> tag.
Changing leading zeroes to leading spaces:
$formatted_value = sprintf("%' 6s", $phpPartHrsMls);
Try str_pad.
str_pad($phpPartHrsMls, 6, " ", STR_PAD_LEFT);
you use %06d please try some larger number .
your code can some thing like below try :
printf('%20.2f', $phpPartHrsMls);
and you can use for space on your html .
i want to read a text file on a php page and print (echo) the content in the same form (with the paragraphs). So i tried two implementations
Code 1
$textfile = "teste.txt"; // Declares the name and location of the .txt file
$content="";
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w ');
if (is_readable($textfile)) {
$content .= fread($fh, 8192);
echo $content;
} else {
echo 'The file is not readable.';
}
fclose($fh);
?>
Using this code nothing apper on the screen. and i had another problem. If i used filesize ($textfile) on the fread i had this error Length parameter must be greater than 0.So i guess this is the problem. but the text file has content
Code 2
<?php
$homepage = file_get_contents('teste.txt');
echo $homepage;
?>
This code works. but the format is not what i want.
the echo prints this
25 ºC 26 ºC 27 ºC 26 ºC 26 ºC
I want that appear one value on each line like i have on the text file.
What can i do to acomplish that
Thanks for the help
note sure if this is answerd, but try this
<?php
$file = "LOCATION/NAME.txt";
$text = file_get_contents($file);
$text = nl2br($text);
echo $text;
?>
This should be as simple as wrapping the output inside the PRE tag:
<?php
$homepage = file_get_contents('teste.txt');
echo '<PRE>' . $homepage . '</PRE>';
?>
This will work better than nl2br() if you also need to keep the exact number of spaces due to needing a fixed-width font. HTML normally turns two or three spaces in a row into just one. But inside a PRE tag if you have 3 spaces, all 3 will be displayed.
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>";
I have a string with XML:
$string =
"
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
";
And would like display it on my website like this:
This is XML string content:
<shoes>
<shoe>
<shouename>Shoue</shouename>
</shoe>
</shoes>
So I would like to do it:
on site, not in textbox
without external libraries, frameworks etc.
formatted with proper new lines
formatted with tabs
without colors etc., only text
So how to do it in plain and simple way?
If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:
<?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>
you can use htmlentities(), htmlspecialchars() or some similar function.
It should work like that:
echo '<p>This is XML string content:</p>'
echo '<pre>';
echo htmlspecialchars($string);
echo '</pre>';
If it is a SimpleXMLobject
<pre>
<?php
echo htmlspecialchars(print_r($obj,true));
?>
</pre>
I searched for a solution to have slightly coloured output:
$escaped = htmlentities($content);
$formatted = str_replace('<', '<span style="color:blue"><', $escaped);
$formatted = str_replace('>', '></span>', $formatted);
echo "<pre>$formatted</pre>\n";