Code:
<?php
echo "Hello World!\nSecond line";
?>
I'm trying to display this as two separate lines but the line break character doesn't work and instead a space gets printed inbetween the two. I've tried \r\n as well as .PHP_EOL. as well as placing the string in single quotes. None of them seems to work. So how do I print new lines in PHP?
I'm working on phpDesigner 8
Use nl2br() to turn the new lines into HTML <br/> elements:
<?php
echo nl2br("Hello World!\nSecond line");
?>
The linebreaks are actually created but you just don't see them. Change the Content-Type header to see the original result:
header('Content-Type: text/plain');
This is very useful when debugging. You could also view the page source in your browser to see the original output.
And to answer your question, the most easiest way to do this would be to use nl2br() (as has been suggested in John's answer above).
As you are printing in HTML, you should use the <br /> tag instead of \n
Related
I manually execute a query and copy paste the output to an editor.Now it is well aligned like:
Hi,
This is sample data.
Thanks.
If I stored the output of the query in a php varibale and print it using echo command it is not aligned. It looks like
Hi, This is sample data. Thanks.
Is it possible to print the output as it is in php?
you need <pre> tag for that. but I suggest you should format it with proper html
echo "<pre>".$youvariable."</pre>";
PHP prints it perfectly fine. If you're looking at it in a browser though, consecutive spaces and newlines are collapsed into a single space by the browser. Replace newlines with <br> tags to keep them, using nl2br.
Okay, so I've got a program that converts nl2br, and prints the output to a console window. Though it prints along with the output data, the <br />. I'm fine with it and all, if I can't remove/hide it without all the output melding together, but I'd rather hide/remove it if possible. Any suggestions are thankfully accepted.
-Example-
What console says:
: Output here!<br />
What I want:
: Output here!
I've tried substr($out, 5), trim(), and that's all I could come up with. All those did was meld the output together.
It's not totally clear, but I suspect you misuderstood nl2br. As the name suggests, it adds for each "newline" a "br" before, so that in HTML (which treats newlines like spaces in text) you will see actualy the text continuing in the next line. When you print to console, the console interprets usually the newline as a newline and so a new line begins. You do not have to use nl2br if you want to output "it" to console. (See nl2br for details).
That's what nl2br() does, changes new line character \n to <br />.
If you want, take those out, you can use str_replace()
str_replace("<br \/>", "", $output);
I have a system set up for users to submit their articles into my database. Since it will just be HTML, I don't want to expect them to know to type <br /> every time there's a newline, so I am using the PHP function nl2br() on the input.
I'm also providing an article modification tool, which will bring their articles back into the form (this is a different page, however) and allow them to edit it. In doing this, the <br /> elements were appearing also (with newlines still). To remedy the elements appearing (which I had expected, anyway) I added preg_replace('/<br(\s+)?\/?>/i', "\n", mysql_result($result,$i,"content")) which I had found in another question on this site. It does the job of removing the <br /> elements, but since it is replacing them with newlines, and the newlines would have remained originally anyway, every time the post is edited, more and more newlines will be added, spacing out the paragraphs more and more each time. This is something a user won't understand.
As an example, say I enter the following into the article submission form:
Hello, this is my article.
I am demonstrating a new line here.
This will convert to:
Hello, this is my article.<br />
I am demonstrating a new line here.
Notice that, even though the newline character was converted, there is still a newline in the text. In the editing form, the <br /> will be converted back to newline and look like this:
Hello, this is my article.
I am demonstrating a new line here.
Because the <br /> was converted to a newline, but there was already a newline. So I guess what I'm expecting is for it to originally be converted to something like this:
Hello, this is my article.<br />I am demonstrating a new line here.
I'm wondering ... is there a way to stop the nl2br() function from maintaining the original newlines? Might it have to do with the Windows \r\n character?
The function you're using, nl2br is used for inserting them, but not replacing them. If you want to replace \n with <br /> you just need to use str_replace. Like so:
$string = str_replace("\n","<br />",$string);
There is absolutely no need for regex in this situation.
It seems like the problem you described is not a bug, but a feature of bl2br. You could just write your own function for it, like:
<?php
function NlToBr($inString)
{
return preg_replace("%\n%", "<br>", $inString);
}
?>
I found this one in the comments of the documentation of the nl2br-function in the PHP Manual: http://php.net/manual/de/function.nl2br.php. If the one I posted did not work for you, there should be plenty more where it came from.
(Or just use the function from the other Answer that was just posted, I guess that should work, too)
This should fix it:
preg_replace('/<br(\s+)?\/?>(?!\s*\n)/i', "\n", mysql_result($result,$i,"content"))
You cannot simply remove the breaks, because they might be on the same line. This regex will replace all breaks with newline but not those that are followed by the newline.
It will leave the <br>\n in the text. Additional regex will get rid of them:
preg_replace('/<br(\s+)?\/?>/i', "", $res)
For some reason I can't use \n to create a linefeed when outputting to a file with PHP. It just writes "\n" to the file. I've tried using "\\n" as well, where it just writes "\n" (as expected). But I can't for the life of me figure out why adding \n to my strings isn't creating new lines. I've also tried \r\n but it just appends "\r\n" to the line in the file.
Example:
error_log('test\n', 3, 'error.log');
error_log('test2\n', 3, 'error.log');
Outputs:
test\ntest2\n
Using MAMP on OSX in case that matters (some sort of PHP config thing maybe?).
Any suggestions?
Use double quotes. "test\n" will work just fine (Or, use 'test' . PHP_EOL).
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:
http://php.net/manual/en/language.types.string.php
\n is not meant to be seen as a new line by the end user, you must use the html <br/> element for that.
/n only affects how the html that is generated by php appears in the source code of the web page. if you go to your web page and click on 'view source' you will see php-generated html as one long line. Not pretty. That's what \n is for ; to break that php-generated html into shorter lines. The purpose of \n is to make a prettier 'view source' page.
When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.
<?php
echo "Line 1\nLine 2";
?>
This will render in your browser as:
Line 1 Line 2
If you need to send plain text to your browser, you can use something like:
<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>
This will output:
Line 1
Line 2
nl2br() function use for create new line
echo nl2br("Welcome\r\n This is my HTML document", false);
The above example will output:
Welcome
This is my HTML document
I'm pretty sure you are outputting to a html file.
The problem is html ignores newlines in source which means you have to replace the newlines with <br/> if you want a newline in the resulting page display.
You need to use double quotes. Double quotes have more escape chars.
error_log("test\n", 3, 'error.log');
error_log("test2\n", 3, 'error.log');
to place the \n in double quotes try
$LOG = str_replace('\n', "\n", $LOG);
It's because you use apostrophes ('). Use quotationmarks (") instead. ' prompts PHP to use whatever is in between the apostrophes literally.
Double quotes are what you want. Single quotes ignore the \ escape. Double quotes will also evaluate variable expressions for you.
Check this page in the php manual for more.
The “\n” or “\r” or similar tags are treated as white-space in HTML and browsers. You can use the "pre" tag to solve that issue
<?php
echo "<pre>";
echo "line1 \n some text \t a tab \r some other content";
echo "</pre>";
?>
If you want to print something like this with a newline (\n) after it:
<p id = "theyateme">Did it get eaten?</p>
To print the above, you should do this:
<?php
print('<p id = "theyateme">Did it get eaten?</p>' . "\n");
?>
The client code from above would be:
<p id = "theyateme">Did it get eaten?</p>
The output from above would be:
Did it get eaten?
I know it's hard, but I always do it that way, and you almost always have to do it that way.
Sometimes you want PHP to print \n to the page instead of giving a newline, like in JavaScript code (generated by PHP).
NOTE about answer: You might be like: Why did you use print instead of echo (I like my echo). That is because I prefer print over echo and printf, because it works better in some cases (my cases usually), but it can be done fine with echo in this case.
How do you print a new line break in CakePHP.
I have tried this out:
echo "<b>\nhelloworld\n</b>";
instead of printing the it into three separate lines like this way:
<br>
helloworld
</b>
it just printed in this way when I viewed the HTML source code:
<b>helloworld</b>
Try \r\n instead of \n.
You could initially try just pressing enter and see if it's picked up...
If that doesn't work
Try doing it like this.
echo "<b>"."\n"."helloworld"."\n"."</b>";
that indeed is exactly how you add line breaks. What are you using to view the source code? Some tools, such as Firebug, normalise and reformat the source code for you which is why you might not be seeing the breaks.
You have to escape the backslash:
echo "<b>\\nhelloworld\\n</b>";
Your original line of code worked fine for me. I see that Anax solved your problem, above, but I wonder why carriage returns should be necessary but only in some circumstances?