I have a textarea form in my html. If the user hits enter between 2 sentences that data should be carried over to my PHP.
Currently if the user enters:
Apple
Google
MS
and my PHP code is:
$str = $_POST["field"];
echo $str;
I get
Apple Google MS
as the output. I want output to be like this
Apple
Google
MS
what should I do?
Try nl2br() instead:
echo nl2br($str);
The newlines should be included in the string that you get from $_POST["field"]. However, if you then use that string as output in HTML, newlines will be treated as whitespace. To force the line breaks, use preg_replace("/\n/", "<br />", $str).
This is because when you echo it it's being displayed as HTML. A \n character is interpreted as a space. If you view the source you'll see your desired output.
To convert \n to <br> use:
echo nl2br( $str );
Related
I'm having some trouble getting nl2br to do what I want.
Can someone explain why nl2br doesn't change the \n in the JSON data to < br /> in my PHP?
Here is the code:
$page = file_get_contents('JSON_FEED_URL');
$page2 = nl2br($page);
When I echo $page2 and view the HTML page it comes out as a big wall of text.
Try
$page = file_get_contents('JSON_FEED_URL');
$page2 = preg_replace("/\\n/m", "<br />", $page);
As said, str_replace would also work a tad faster, but the above counts of multiline breaks.
nl2br does not replace the new lines, only ads the <br> tags. In HTML there is no need to remove the new line characters as they are considered to be white space which is collapsed to a single space for display. This fact is the very reason for having the <br> tag.
Since you say that you can see the \ns when echoing (instead of a newline in the source), this probably means that your \ns are literal, and not "proper" newlines. This is because your JSON is read as a string. Fix this by calling json_decode();
$page2 = nl2br(json_decode($page));
Explanation:
The string
line1
line2
is in JSON saved as
"line1\nline2"
but that \n is not a real newline, just normal characters. By decoding the JSON, it will be correct.
nl2br did not interpret \n to <br /> in HTML because they were literal slashes followed by n.
On your source, the text looks like the following:
FRIDAY THROUGH WEDNESDAY.\n\nMORE RAIN IS
Should be something similar to the ff so that it'll be interpreted:
FRIDAY THROUGH WEDNESDAY.
MORE RAIN IS
You can address your problem by using str_replace() or if you can update your code when putting content on "JSON_FEED_URL", add nl2br before putting those content.
I try this:
echo nl2br($row['content']);
But what I get is:
Hello everybody\n Good luck!
Why doesn't it convert the \n? the database is storing data as UTF-8.
In addition, I check it with a test string, and found out that if the string is with double quotes it doesn't work too.. I mean:
echo nl2br("Hello everybody\n Good luck");
It should work but if you wish you can try str_replace("\n","<br />") instead of nl2br.
The issue is that the text is being stored with an additional slash in your database. Use the stripslashes (PHP doc here) function on the text before nl2br (PHP doc here)
$myText = "Hello\\nWorld"; //text from a database (with line breaks escaped)
stripslashes(nl2br($myText));
print $myText;
// Result: Hello<br>World
I guess you have written to database \n as two characters, so it's not a "new line" character.
Try echo str_replace('\n','<br />', $row['content'])
I need help! Please, I use message form like facebook and twitter, new message slidedown effect. I used line break but shown "n" in new message. When refresh browser, Line break not shown "n". Sorry for my English used. :D
If you are using \n to put line break you have to enclose it in "
"Text \n this is on new line"
Have in mind that for HTML output you have to use <br />
Assuming that you're displaying messages which contain newlines (\n) and you want to display these in HTML output, keeping the newlines, then you should use nl2br to convert the newlines to HTML line break (BR) tags.
In PHP you can print line-break with the PHP_EOL constant without use /n
example:
$string = "First Line".PHP_EOL."Second Line";
echo $string
the output is:
First Line
Second Line
Try either \r\n or <br/>.
Cheers
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.
Well, I am abit confuse using these \r,\n,\t etc things. Because I read online (php.net), it seems like works, but i try it, here is my simple code:
<?php
$str = "My name is jingle \n\r";
$str2 = "I am a boy";
echo $str . $str2;
?>
But the outcome is "My name is jingle I am a boy"
Either I put the \r\n in the var or in the same line as echo, the outcome is the same. Anyone knows why?
Because you are outputting to a browser, you need to use a <br /> instead, otherwise wrap your output in <pre> tags.
Try:
<?php
$str = "My name is jingle <br />";
$str2 = "I am a boy";
echo $str . $str2;
?>
Or:
<?php
$str = "My name is jingle \n\r";
$str2 = "I am a boy";
echo '<pre>' .$str . $str2 . '</pre>';
?>
Browsers will not <pre>serve non-HTML formatting unless made explicit using <pre> - they are interested only in HTML.
Well in your example you've got \n\r rather than \r\n - that's rarely a good idea.
Where are you seeing this outcome? In a web browser? In the source of a page, still in a web browser? What operating system are you using? All of these make a difference.
Different operating systems use different line terminators, and HTML/XML doesn't care much about line breaking, in that the line breaks in the source just mean "whitespace" (so you'll get a space between words, but not necessarily a line break).
You could also use nl2br():
echo nl2br($str . $str2);
What this function does is replace newline characters in your string to <br>.
Also, you don't need \r, just \n.
In HTML, spaces, tabs, linefeeds and carriage returns are all equivalent white space characters.
In text, historically the following combinations have been used for newlines
\r on Apple Macs
\r\n on Windows
\n on Unix
Either use \n (*NIX) or \r\n (DOS / Windows), \n\r is very uncommon. Once you fix that, it should work just fine.
Of course, if you're outputting HTML, a line break does nothing unless it's inside <pre></pre> tags. Use <br /> to separate lines in HTML. The nl2br() function can help you to convert line breaks to HTML if needed.
Also, if you use single-quoted strings (your example has double quoted strings), \r and \n will not work. The only escape characters available in single quoted strings are \' and \.
Are you displaying the results in an HTML page? if so, HTML strips whitespace like newlines. You'd have to something like use '<br />' instead of '/r/n' in HTML.