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?
Related
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
ok i do have this following codes
<?php
ob_start();
?>
codepad is an
online compiler/interpreter,
and a simple collaboration tool.
Paste
your code below,
and codepad wi
ll run
it and give you a short
URL you can use to share
it in chat or email
<?php
$str = str_replace('\r\n','',trim(ob_get_clean()));
echo $str;
?>
and you can see how it works here
http://codepad.org/DrOmyoY9
now what i want here is to remove the newlines from the stored output of the ob_get_clean().
I almost looked around the internet on how to remove newlines in the strings and that's the common and fastest method to remove the newlines aside from using the slowly preg_replace().
Why this happens? is this already a bug? or i just missed something?
\r\n is windows style, but if the user using linux or mac, it would be different . so best solution is:
$str = str_replace(array("\r","\n"),'',trim(ob_get_clean()));
I think u miss one thing, it should be :
$str = str_replace("\r\n",'',trim(ob_get_clean()));
using double quotes not single quotes
I am making a comments system in which i can accept user input with line breaks.
I don't want to show the \n or \r thing
Please help me with this
nl2br($string);
is fast and easy
They are enabled by default. If you are outputting the text to a web browser, make sure to use nl2br or the white-space attribute in CSS.
using preg_replace
simply replace it
preg_replace('/\n/'," ",$str);
\n should do the trick.
if you are trying to output a textarea, then use nl2br();
also:-
If you are trying to format your HTML source, you should use the constant PHP_EOL. The main reason being that on windows machines the EOL is \r\n and on UNIX machines it is \n. With a properly installed LAMP set up just use PHP_EOL like so.
$html.="<p>This is my HTML</p>" . PHP_EOL;
Line breaks will be stored just like any other character.
\n is an escape code that allows you to explicitly insert a line break into a string, but I don't think that it's relevant here.
The issue you're actually facing is that HTML does not impart any visual meaning to a line break. Line breaks within HTML code do not, under normal circumstances, equate to a line break on the screen.
One way to render a line break in HTML is to use a line break tag, or <br>.
In PHP, you can automatically convert line breaks to <br> with the nl2br function. Applying this to your comment text when you output it into HTML will enable you and other visitors to see the line break visually.
I have a text area in HTML where the user can enter text, but when the form is submitted, and its passed to a php script which echo's it, there aren't any newlines. Knowing that HTML does that, I tried doing a preg_replace() before echoing it...
echo preg_replace("/\n/", "<br />", $_GET["text"]);
but still everything is on one line.
So my best guess is that HTML textareas use a different newline character... can anybody shed some light on the subject?
EDIT
Ok, so I've figured out the problem: The Javascript is stripping the newlines. view code here
EDIT 2
Ok, so thanks to Jason for solving this problem. I needed to do:
escape(document.getElementById('text'));
Instead of just:
document.getElementById('text');
and the newlines are preserved, problem solved!
echo nl2br($_GET['text'])
Though, your preg_replace worked for me!
usually when testing for newlines in any string, I use /[\n\r]/, just to cover my bases. My guess is that this would match the new lines.
Edit: The problem described below was just caused by a "feature" of my IDE, so there's actually nothing wrong with the regex. If you're interested in how to double line breaks, here's your question and answer in one neat package. :)
I want to change every line break in a string to be two line breaks:
"this is
an example
string"
// becomes:
"this is
an example
string"
However, it needs to take Unix/Windows line endings into account. I've written the code below, but it's not behaving itself.
$output = preg_replace("/(\r?\n)/", "$1$1", $input);
But it's not working. Changing the replacement string to this...
"$1 $1"
...makes it work, but then I have an unwanted space in between.
That's interesting. I just tested your sample code on two different UNIX systems (Ubuntu and a FreeBSD box, for the record). In both cases, it worked exactly as you say you wish it to. So your platform or your configuration may be partially at fault.
Wait Wait. Are you just directly outputting that to the browser? Did you View Source? Returns are not shown in HTML. Try putting <pre> before and </pre> after, if you want to view the returns as line breaks.