PHP text formating using define function - php

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.

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 \n new line no show well

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"

How to use nl2br to remove \n and add breaks

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.

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

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