php \n new line no show well - php

I want to show this line to the end when go to submit texarea , for save little data i use txt db and i have problem because the line with \n no get replace this character
For example
:
$replace=str_replace("\n","<br>",$val);
$replace=str_replace("\r","<br>",$val);
$replace=str_replace("\n\r","<br>",$val);
Replace \n by <br> but inside the text no see only line i see this
data1,data2,data3,data4,data5<br>
hello
<br>
yes
<br>
And this it´s bad because i need show all in only line
Thank´s Regards !!!

If I understand well, you try to replace the \n by a HTML <br> ? If it is, you can use the nl2br function of PHP:
http://php.net/manual/en/function.nl2br.php

Ever considered using:
echo nl2br($Val);
Which makes your code more graceful than looking at:
$replace=str_replace("\n","<br>",$val);
$replace=str_replace("\r","<br>",$val);
$replace=str_replace("\n\r","<br>",$val);
as nl2br does exactly what your str_replace lines are doing and makes it easier.
This converts all the line break formats to the HTML <br>

Try this:
$replace=str_replace("\\\n","<br>",$val);
$replace=str_replace("\\\r","<br>",$val);
$replace=str_replace("\\\n\\\r","<br>",$val);
For example, \n sends literal character n.
However, \\n is interpreted as \ (literal character "\") and \n (literal character "n") or altogether, literal "\n"

Related

escape sequences are not working in php

if im not wrong \n representation means that a newline as <br> .But when i use <br> or another tags they work properly but escape sequences.
example
echo "write somethings<br>";
echo "about coding";
above example works fine but when i try to use escape sequences none of them are not working
echo "write something\n";
echo "about coding";
it's just an example for newline character and the other escaping characters dont work as \n.What is the real logic on this case?
\n and other similar escape sequences are not part of HTML. You should use HTML escape sequences. These can be found here: http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
So only your <br> tag works but \n is not
No, this is an example of HTML rules.
Putting \n in a PHP string and then outputting it as HTML will put a new line character in the HTML source code. It's just line pressing return when writing raw HTML.
HTML puts no special meaning on the new line character (at least outside of script elements and elements with various non-default values of the CSS white-space property) and treats it like any other white space character.
<br>, on the other hand, is a line break element (but usually an indication that you should be using a block level element around the content instead).
HTML ignores carriage return and linefeed characters, treating them as whitespace. If you want to use display a string formatted with "\n" you can use nl2br to convert it, e.g.
echo nl2br("this is on\ntwo lines");
If you look at this in the browser it wont work : browser knows only HTML for display (<br>) but not escape like \n or \r

PHP text formating using define function

I need to display some text using define, but the text includes newlines. When I enter the text as an argument, it is not displayed with the newlines. Is there any way to fix this?
define('TEXT_INFORMATION', 'place for the text');
Thank you
Use nl2br
( http://php.net/manual/en/function.nl2br.php )
It replaces the newlines (\n, \r, \n\r, \r\n) to breaks ( <br /> )
/* Little Example */
define('TEXT_INFORMATION', 'Line 1
Line 2
Line 3');
echo nl2br(TEXT_INFORMATION);
You can use something like this:
define('TEXT_INFORMATION', nl2br(<<<EOD
This is a text
with multiple lines
EOD
));
In order for PHP to recognise escape characters you need to use double quotes:-
define('TEXT_INFORMATION', "Place for text<br/>\n");
echo TEXT_INFORMATION;
See the manual for more information on using escape sequences.
try
define("TEXT_INFORMATION", "place for the text \n");
http://www.trans4mind.com/personal_development/phpTutorial/quotes.htm
If you want to put in newlines you can use \r and \n (and \r\n). Though I think you'd have to use "" for \r and \n to work.
You can also use nl2br to have the newlines be converted to tags.

Why LineBreak shown "n" in php?

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

PHP Linefeeds (\n) Not Working

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.

Replacing a new line with its html equivalent in PHP

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.

Categories