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.
Related
I went through similar questions on this issue but couldn't find a solution.
I have a simple textarea in which text will be pasted or entered directly.
How do I preserve those line breaks?
Right now line breaks will just be ignored, but when I print_r($_POST) and inspect the output in the Chrome Developer Console it still has the original text breaks.
How can I display those when showing the submitted data?
Right now line breaks will just be ignored
No, it is still there. The key you miss is that what you enter goes as ASCII code LF (0xA) but this makes no effect in HTML unless the text is displayed as preformatted block (<pre> ... </pre>). If not, then line break is represented bytag so what you need to do if you want to display entered text is to convertLFs to(you can usenl2br()function to do that) or use` block.
You can use nl2br() function:
Inserts HTML line breaks before all newlines in a string
echo(nl2br($_POST));
More information at http://php.net/manual/en/function.nl2br.php.
As Marcin noted, use nl2br() when displaying your data as instructed in this answer: https://stackoverflow.com/a/6480671/6206601
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!
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.
I'm using PHP to create some basic HTML. The tags are always the same, but the actual links/titles correspond to PHP variables:
$string = '<p style="..."><strong><i>'.$title[$i].'</i></strong>
<br>';
echo $string;
fwrite($outfile, $string);
The resultant html, both as echoed (when I view the page source) and in the simple txt file I'm writing to, reads as follows:
<p style="..."><a href="http://www.example.com
"><strong><i>Example Title
</i></strong></a></p>
<br>
While this works, it's not exactly what I want. It looks like PHP is adding a line break every time I interrupt the string to insert a variable. Is there a way to prevent this behavior?
Whilst it won't affect your HTML page at all with the line breaks (unless you are using pre or text-wrap: pre), you should be able to call trim() on those variables to remove newlines.
To find out if your variable has a newline at front or back, try this regex
var_dump(preg_match('/^\n|\n$/', $variable));
(I think you have to use single quotes so PHP doesn't turn your \n into a literal newline in the string).
My guess is your variables are to blame. You might try cleaning them up with trim: http://us2.php.net/trim.
The line breaks show up because of multi-byte encoding, I believe. Try:
$newstring = mb_substr($string_w_line_break,[start],[length],'UTF-8');
That worked for me when strange line breaks showed up after parsing html.
I am facing a bit of a quandary, I need to replace a new line with <br />. Now, clearly, replacing all instances of \n did not work, as the page did not have proper linebreaks. Here is an example of some possible text:
Some text
More text
Now, this is an issue because there is no \n and I have no way to auto-insert <br />. How can I ensure that this contains proper linebreaks?
This is in PHP. I cannot serve it as plain text.
To replace new line breaks with just use nl2br
Maybe you want http://php.net/nl2br? Or maybe I have misunderstood...
You will want to use nl2br.
Your text has to have a newline (\n) or carriage return (\r) if the text is on 2 different lines.
nl2br will handle either case.
php has a built in function for that.
nl2br() i believe.