How to double all line breaks? - php

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.

Related

Do I really need to add "\n" to every PHP statement that I write in a script if I dont want my output to be like totally messed up?

I'm learning PHP and writing it and executing in the browser is cumbersome.
So I write it as a script and execute it on the terminal, such as
me#machine $ php script.php
However, it seems to me, all statements are printed to the same line, if not explicitly a newline character is also printed.
<?php
echo "Hello World.\n";
?>
If I omit \n, I end up with
me#machine $ php hello_world.php
Hello World. > me#machine $
which kind of is lame.
Do I really, like really really (as in "totally really"), need to type \n for every statement I like to test?
You've got a choice:
Include a \n on the end, and have a line feed.
Leave out the \n and don't have a line feed.
It's up to you. No, you don't need to have it there, but if you want to output text to the command line, you probably do want it.
I guess there's one other alternative. Since PHP outputs content that is outside of the <?php .. ?> tags as plain text, you could just put a blank line at the end of your code after the final ?>. That will cause PHP to output a new line at the end without you needing to write \n.
But to be honest, putting the \n in your string is better coding practice. (And frankly, \n isn't exactly the worst thing in the world to having in your code. if you can't cope with the horrors of seeing \n in your code, then you're going to have a hard time reading most program code anyway... just wait to you learn Regex!!!)
No. You might write your own writeLn() function and maybe use PHP_EOL instead of \n, depending on what the script is for. (New-line string differ across systems, and PHP_EOL is your server's version of new-line, so it makes your script portable at least in regard of the running environment.)
Yes, you need to use \n to print a new line.
PHP will only print that which you tell it to print. So, if you want a new line then you need to print a new line.
Depending on your requirements.
\n is representing a new line break.
So if you are testing a single command with a single echo, from command line you might beable to go without the newline break.
If you have multiple echos, all the output will be jumbled into one long text.
function NewLine ($Text)
{
$Text = $Text."\n";
return $Text;
}
echo NewLine('This Text Will Have A New Line Appended To The End');

Why should use linebreak '\n'

I need some advice ...
I'd like to know if it is a good practice to use in a code line breaks "\n"? What is the purpose?
$my_string .= "\n" . "<p>Some values</p>" . "\n";
Till now I haven't use it and I'd really like to know ... your opinion.
Thanks
You only need to use "\n" to make the resulting html code neater/easier to read. It is not necessary.
The new line makes your generated code easier to read. You might think noone should read your code, however if you run into some problems the generated code is easier to read for debugging purposes as well.
\n can be used when streaming over sockets as well. Sometimes you need to use \r, depending on the operating system.
This is very common mistake of the newbie programmers.
They never have an idea that the result of the PHP script execution is plain HTML code.
And sometimes a programmer have to sort things out with that HTML.
While it's just impossible if there are no linegreaks in the code.
Anyway, a good practice would be
$my_string = "Some values";
?>
<p><?=$my_string</p>
so, you won't need no special linebreaks in the PHP code.
Also, there are some cases where you have to use linebreaks.
For example, if you are composing an HTML email message, you hve to add linebreaks, or they will be forcibly added in the unexpected places.
at time of writing to break line, means display text after line-break to next line
like enter in some text editor...
The only purpose is for people using the "View Source" feature of the browser - basically no one. While it is considered good practice, I almost never do (because it's pointless) :)
for pre-tag (pre formatted text):- http://webdesign.about.com/od/htmltags/f/blfaqpre.htm
so, you can do this in SO
1
2
3
4
5
6
7
8
\n is just an escape for a new line. An escape is not really required in PHP, but it makes it seamless to output a new line mark.

How do i enable line breaks in php?

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.

smarty replace line breaks

At the time of writing this, the smarty.net website appears to be down.
Anyway, how do I replace line breaks with a space in a smarty variable? Is it something like this {$var|regex_replace:'[\\r\\n]':'\s'} ? I tried it but it didn't work.
Try this if it works:
{$var|regex_replace:"/[\r\n]/" : " "}
The problem with [\r\n] is that it'll replace a single windows crlf with a double replacement. (this is not a biggie if you just output spaces, but...)
Example:
{$letter="--\n--\r\n--\r\n\r\n--"}
{$var|regex_replace:"/[\r\n]/":"BR"}
result:
--BR--BRBR--BRBRBRBR--
Consider if you want to replace line breaks with html newlines; the above will create a mess. This is what works as expected:
{$var|regex_replace:"/\r*\n/":"<br>"}
(Btw; if you consider nl2br for the above; it won't replace newlines, it'll just add a br to each - that can be a problem in some cases)
Now, the classic mac newline is just a carriage return, so more tweaking would be needed for that, but it's probably not really existent anymore.

How to print line break in cakePHP?

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?

Categories