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");
Related
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
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>";
This code:
<?php
echo "This is the first line. \n";
echo "This is the second line.";
?>
Echoes out all on the same line: ("This is the first line. This is the second line.")
Shouldn't \n have basically the same function as <br> ? What am I missing here?
HTML doesn't render \n, a \n will put a break in your HTML source code
PHP
<?php
echo "<p>This is the first line. \n";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.
This is the second line.</p>
HTML
This is the first line. This is the second line.
PHP
<?php
echo "<p>This is the first line.";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.This is the second line.</p>
HTML
This is the first line.This is the second line.
PHP
<?php
echo "<p>This is the first line. <br/>";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.<br/>This is the second line.</p>
HTML
This is the first line.
This is the second line.
You mix up the output of php and the result in your browser. \n is newline. You may see it, when you read the source code in your browser(Ctrl + U in Chrome) .
But browser only render <br> as newline on webpage
echo "This is the first line. \n";
Will produce a linebreak in your source, meaning the cursor is currently on the next line (the line belove the text).
echo "This is the second line.";
Will produce a single line and leave the cursor right after the text (on the same line).
echo "This is the second line.<br />";
Will produce a single line but in rendered html containing a visible linebreak. However, in the sourcecode there will be no linebreaks, so:
echo "Line one<br />Line two";
Will render two lines in html but one line in the source.
echo "This is the first line.", '<br />', PHP_EOL;
^ HTML code for BR and ENTER.ENTER is visible in the source or PRE tags, or TEXTAREAs, or when enabled by CSS white-space (etc.) while BR is the line break in HTML.
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>";
?>
I am trying to do a line break after the message "Original message", I have tried with this but It keeps showing me the
---Original message---<br />
message
<textarea id="txtMessage" rows="10" cols="50"><?php echo nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']));?></textarea>
I want something likes this:
---Original message---
message
any advise?
This should do what you want it to:
<?php echo str_replace('<br />', " ","---Original message---\n".$array['message']);?>
nl2br — Inserts HTML line breaks before all newlines in a string (from php.net)
Example:
echo "<textarea>HI! \nThis is some String, \nit works fine</textarea>";
Result:
But if you try this:
echo nl2br("<textarea>HI! \nThis is some String, \nit works fine</textarea>");
you will get this:
Therefore you should not use nl2br before saving it to database, otherwise you have to get rid of <br /> every time you try to edit text! Just use it when you print it out as text.
echo nl2br(str_replace('<br/>', " ", ... ));
should be
echo str_replace('<br />', ' ', ... );
The php function "nl2br" takes newlines, and converts them into br tags. If you don't want that, you should probably remove it :).
Heh, beaten by Ryan.
You're trying to replace <br/>, but the original text has <br /> (note the space).
You are removing the HTML breaks, then adding them back! Look at your code:
nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']))
First, str_replace replaces '<br/>' with a space. Then, nl2br adds a <br> for every newline (\n) it finds.
Remove nl2br call and it's done.
If you want to do nl2br on all of the text except for what's inside the textarea you could do this:
function clean_textarea_of_br($data) {
return str_replace(array("<br>", "<br/>", "<br />"), "", $data[0]);
}
$message = preg_replace_callback('#<textarea[^>]*>(.*?)</textarea>#is',clean_textarea_of_br,$message);