i'm using metacharacter in a simple php code, but it is now showing Second line of text into a new line.
echo "First line of text \r\n Second line of text";
I know Unix based systems use just a "\n". But as i'm on Windows 7, so i am using "/r/n"
You can use :
echo nl2br("First line of text\n Second line of text");
nl2br function
Or you can use EOL in php
Try using other way with basic HTML tag <br />:
echo "First line of text" . "<br />" . "Second line of text";
Related
Why is the following code not working...?
$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
}
It prints
hello world !
Instead of
hello
world
!
Thanks
You also have to echo <br />
The HTML <br /> element produces a line break in text (carriage-return).
It is useful for writing a poem or an address, where the division of
lines is significant.
$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
echo "<br />";
}
doc: <br />
Another option is to use nl2br to inserts HTML line breaks before all newlines in a string
$test = "hello \n world \n !";
echo nl2br($test);
Doc: nl2br()
Add new line to Browser Output via HTML
In HTML, you need to add line break for new line via HTML Tag. This will not appear as required output if there is line break in text/string.
<br> used to add single line break.
Replace
echo $line;
Into
echo $line."<br>";
Add new line to string/source code
If you need to add new line into source code only then replace echo $line; with echo $line."\n";
\n is used into double quotes for new line.
Add new line to Browser Output via PHP
Anyways an other way to do this via PHP is to use PHP's built-in method nl2br()
$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
}
The result is:
hello world !
Basically you have split on the newlines, and the newlines are not included as you print out each split part.
Use nl2br($line) and replace the delimiter with empty space.This way your new line will be visible in html:
$test = "hello \n world \n !";
foreach(explode(" ",$test) as $line){
echo nl2br($line);
}
will output:
hello
world
!
and the output html:
hello<br />
world<br />
!
This code:
<?php
echo "This is the first line. \n";
echo "This is the second line.";
?>
Echoes out all on the same line: ("This is the first line. This is the second line.")
Shouldn't \n have basically the same function as <br> ? What am I missing here?
HTML doesn't render \n, a \n will put a break in your HTML source code
PHP
<?php
echo "<p>This is the first line. \n";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.
This is the second line.</p>
HTML
This is the first line. This is the second line.
PHP
<?php
echo "<p>This is the first line.";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.This is the second line.</p>
HTML
This is the first line.This is the second line.
PHP
<?php
echo "<p>This is the first line. <br/>";
echo "This is the second line.</p>";
?>
HTML Source
<p> This is the first line.<br/>This is the second line.</p>
HTML
This is the first line.
This is the second line.
You mix up the output of php and the result in your browser. \n is newline. You may see it, when you read the source code in your browser(Ctrl + U in Chrome) .
But browser only render <br> as newline on webpage
echo "This is the first line. \n";
Will produce a linebreak in your source, meaning the cursor is currently on the next line (the line belove the text).
echo "This is the second line.";
Will produce a single line and leave the cursor right after the text (on the same line).
echo "This is the second line.<br />";
Will produce a single line but in rendered html containing a visible linebreak. However, in the sourcecode there will be no linebreaks, so:
echo "Line one<br />Line two";
Will render two lines in html but one line in the source.
echo "This is the first line.", '<br />', PHP_EOL;
^ HTML code for BR and ENTER.ENTER is visible in the source or PRE tags, or TEXTAREAs, or when enabled by CSS white-space (etc.) while BR is the line break in HTML.
How can I make text appear on diff lines in html?! They appear on diff lines in my text area when output from mysql, and also appear on diff lines inside mysql. But in the web page its all on one line. How can this be solved?
Use:
string nl2br ( string $string [, bool $is_xhtml = true ] )
It can handle \n, \r, \r\n or \n\r linebreaks and replaces them with a <br />
Example:
$yourText = "This is line one.\nThis is line two.";
$yourText = nl2br($yourText);
echo $yourText;
This will result in:
This is line one.<br />This is line two.
Link to the manual for more information.
Use the PHP nl2br function:
$string = "hello \n world";
$string = nl2br($string);
It's quite self-explanitory: \n gets replaced with <br />
Replace Linebreaks with <br>
str_replace(.N, '<br>', [element]);
You can use <pre>..Your text..</pre> tag for that.
I know for sure that this was already been asked before but I just googled around and couldn't find anything (maybe wrong word choice?).
Just don't be too mad at me, I'm getting mad too...
I'd like
$string = '
This is a line
This is another line
';
to be shown to the HTML page as
This is a line
This is another line
when I do echo $string;.
How can I capture the return key or the new line and replace it with <br>?
Try the nl2br() function:
echo nl2br($string);
Would return:
<br>
This is a line<br>
This is another line<br>
To trim off the leading and trailing new lines, use trim():
echo nl2br(trim($string));
Would return:
This is a line<br>
This is another line
You can use the PHP function nl2br. It doesn't replace the newlines but, rather, inserts <br /> next to them (which is perfectly fine for your purposes).
Using your example:
$string = '
This is a line
This is another line
';
echo nl2br($string);
/* output
<br />
This is a line<br />
This is another line<br />
*/
Use nl2br function like this:
echo nl2br($string);
If you're not getting it with nl2br, your new line character must not be \n.
print nl2br( str_replace( array( "\r\n", "\r" ), "\n", $string);
Why don't you use:
$string = "
This is a line\n
This is another line
";
?
or use
$string = <<<EOF
This is a line
This is another line
EOF;
I am trying to do a line break after the message "Original message", I have tried with this but It keeps showing me the
---Original message---<br />
message
<textarea id="txtMessage" rows="10" cols="50"><?php echo nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']));?></textarea>
I want something likes this:
---Original message---
message
any advise?
This should do what you want it to:
<?php echo str_replace('<br />', " ","---Original message---\n".$array['message']);?>
nl2br — Inserts HTML line breaks before all newlines in a string (from php.net)
Example:
echo "<textarea>HI! \nThis is some String, \nit works fine</textarea>";
Result:
But if you try this:
echo nl2br("<textarea>HI! \nThis is some String, \nit works fine</textarea>");
you will get this:
Therefore you should not use nl2br before saving it to database, otherwise you have to get rid of <br /> every time you try to edit text! Just use it when you print it out as text.
echo nl2br(str_replace('<br/>', " ", ... ));
should be
echo str_replace('<br />', ' ', ... );
The php function "nl2br" takes newlines, and converts them into br tags. If you don't want that, you should probably remove it :).
Heh, beaten by Ryan.
You're trying to replace <br/>, but the original text has <br /> (note the space).
You are removing the HTML breaks, then adding them back! Look at your code:
nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']))
First, str_replace replaces '<br/>' with a space. Then, nl2br adds a <br> for every newline (\n) it finds.
Remove nl2br call and it's done.
If you want to do nl2br on all of the text except for what's inside the textarea you could do this:
function clean_textarea_of_br($data) {
return str_replace(array("<br>", "<br/>", "<br />"), "", $data[0]);
}
$message = preg_replace_callback('#<textarea[^>]*>(.*?)</textarea>#is',clean_textarea_of_br,$message);