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
Related
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"
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 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.
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.
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.