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.
Related
So, I have a taxtarea where the user makes a blog post but when the user submits it their line breaks get replaced with a 'rn' and I don't know why. I thought it was the php script but when I rewrote it and after taking away the str_replaces' it still replaced a new line with a rn.
What is happening?
In textarea just like any Text Editor, New Line are carriage (\r) or newline feed (\n) characters or combination of the two (depending on the OS). To convert these characters to linebreak of HTML, use the nl2br() of PHP.
Check PHP Manual for reference.
Try removing any stripslahes(). Stripslashes removes any backslashes and forward slashes. For example, line breaks are being sent as \n or \r and the stripslashes() takes away the backslashes in those, so that's why it says 'rn'.
I had this very problem, and this solution helped me. Good luck!
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');
I'm aware that PHP ignores enters/white spaces and the \n character doesn't work within a string, so how would one do this?
Why do you think \n doesn't work within a string? It does work.
You can do:
"asd\nasd"
Or:
"asd
asd"
Both will work just fine.
However:
'asd\nasd'
has the actual characters \n not a newline, so check which quotes you are using.
But:
'asd
asd'
works fine.
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.
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.