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;
Related
I have descriptions stored in mySQL with line breaks, so when I output them as they should be, I use:
<?
echo nl2br($description);
?>
And it shows:
Line 1
Line 2
Line 3
So far, so good. Now, I want to use that same description for the meta tags and the problem is that even if I output them like:
$old_string = nl2br($description);
$new_string = preg_replace("/<br \/>/"," ",$old_string);
echo $new_string;
I still get these meta tags:
<meta property="og:description" content="Line 1
Line 2
Line 3"/>
How do I make it output all in 1 line, with just a space in between them?
Thank you very much for any help :)
Don't use nl2br for META, but simply replace line-breaks on the string from database. Note that depending on your environment, a linebreak can be represented with \r\n or simply with \n. So try to use the following code:
$onelinestring = str_replace("\r\n", " ", $description);
$onelinestring = str_replace("\n", " ", $onelinestring);
Try:
$singleLine = str_replace("\n", " ", $string);
Without nl2br, just the string fetched from the db.
I have a variable a such $var:
$var = "One, Two, Three";
I can echo the variable without any problems the output is:
One, Two, Three
Is it possible, when echoing the variable to add a line break where there is a ,, so it would look like this?
One,
Two,
Three
If you echo the text to HTML, you can do the following:
echo str_replace(",", ", <br/>", $var);
If you echo the string to a console or a text file through redirection, just use the PHP_EOL constant, which represents the correct end-of-line string for the current platform ie. "\n" for Unix, "\r\n" for Windows:
echo str_replace(",", "," . PHP_EOL, $var);
You can use this:
$var = "One,\nTwo,\nThree";
\n is the line break, and makes sense if you are working through the terminal
You use \n to force a new line when outputting to a terminal.
$var = "One,\nTwo,\nThree";
You can use the HTML <br /> to output a new line on a web browser.
$var = "One,<br />Two,<br />Three";
You can use the str_replace function once you determine which type you want.
Make use of <br> tag
$var = "One, <br>Two, <br>Three";
(or) Make use of str_replace in PHP
<?php
$var = "One, Two, Three";
echo str_replace(',',',<br>',$var); // code replaces all your commas with , and a <br> tag
Explode the string with the comma as separator, then iterate through the resulting array, adding line breaks (with the br tag if outputting to browser, or newline (\n) escape sequence if outputting to terminal) when needed?
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 have the following apprear exactly like this in a mysql field,
Hello there
world
When i format the above using this code:
echo htmlspecialchars($thestring)
It outputs this,
Hello there<br/><br/>world
How can i get it to do line breaks?
I would like to keep using htmlspecialchars to help with the other html chars.
You need to call nl2br to get the line breaks converted to html:
nl2br(htmlspecialchars($text))
http://php.net/manual/en/function.nl2br.php
<![CDATA[<br/>]]>
this did the trick for me
If you get <br> tags in a string after htmlspecialchars() it means that you have exactly <br> tags in your string (NOT \n line breaks) because htmlspecialchars() doesn't convert \n to <br>.
If so, you have to make a double transformation from <br> to \n and back:
Step 1. Transform <br> to \n
Step 2. Run htmlspecialchars()
Step 3. Transform \n to <br>
Example:
<?php
echo
str_replace( // Step 3
"\n",
"<br />",
htmlspecialchars( // Step 2
str_replace( // Step 1
array("<br />", "<br/>", "<br>"), // make sure that you have your version of <br> tag here
"\n",
$text
),
ENT_QUOTES,
'UTF-8'
)
);
?>
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);