I've got a html form with a textarea. After submitting the form I'd like single line breaks, "\n" to be replaced by "<br />" and double line breaks, "\n\n", by "</p><p>". I've tried with str_replace but that does not have the desired effect.
str_replace("\n", "<br /", $string) has the undersired effect of adding "<br />" even after heads (<h1>) or within lists ("<li>"). Is there a solution?
Submit the textarea using:
nl2br($_POST['textareaname'])
preg_replace('/(\<br(\s*)?\/?\>(\s*)?){2}/i', '</p><p>', nl2br($_POST['textareaname']));
First it will replace \n by <br/> and then double <br/> (or <br> or <br /> and space between by </p><p>
Related
I would like to use the text-indent property (or something like this) to add a indentation of the first line of each paragraph.
First the user can write his text in a textarea, then save it in a DB.
When I want to display this text i use :
$exhib = $res->fetch_array();
echo "<div class='infoContent'>". nl2br($exhib['description']) . "</p></div>";
The line return of the user are stored as \n in DB, and modified to <br /> by nl2br. With my CSS :
.infoContent
{
text-indent: 10px;
}
only the first line is indented. (normal behavior).
Q : How can I make this indentation automatic for each line after a <br /> tag ?
I tried a ugly solution, but it doesn't work because empty paragraph section <p></p> doesn't create another line return (in case the user enter 2 line return \n\n).
echo "<div class='infoContent'><p>" . str_replace("<br />", "</p><p>", nl2br($exhib['description'])) . "</p></div>";
I can replace <p></p> tag by <br /> but it seems to be a very bad solution...
EDIT:
JSfiddle
Thanks
\n\n usually means a new paragraph (enter). The white space between paragraphs is CSS and is actually default browser styling (1em I think?). \n is a <br> (shift + enter).
So don't use nl2br() and do it yourself:
$text = '<p>' . htmlspecialchars($text) . '</p>'; // HTML ENCODE!
$text = preg_replace('#\n\n\n*#', '</p><p>', $text); // 2 or more \n
$text = preg_replace('#\n#', '<br />', $text); // all left-over \n
$text = preg_replace('#><#', ">\n<", $text); // if you like </p>\n<p> with a newline between, like I do
http://3v4l.org/b0AhL
This is pretty much what Markdown does (and Textile and those): 1 newline = BR (not exactly in Markdown) and 2 newlines = P. I always use simple Markdown for rendering plain text.
When you submit your textarea, instead of using CSS to indent only the first line, you can use (non-breaking space).
when you submit your text area, I assume you grab it as such:
$userText = $_POST['description']
Well, before you submit to your database, you could use a simple replace - After you grab the text:
$userText = str_replace("\n", "\n ", $userText);
Then submit that to the database. When it comes back, the nl2br will still make the \n into a <br /> and then it won't see the , though the HTML will see them as four spaces (equal to an indent).
It's dirty, but simple!
Reference: http://www.w3schools.com/php/func_string_str_replace.asp
Im using this piece of code:
<?php $newstring = preg_replace("/[\n\r]/", "<br />" , $googlemapsbox_text); echo $newstring; ?>
To replace the \n's. What I would like to achieve: limit the amount of <br /> per line, so when preg_replace finds more than 1 \n per line, they ALL should be replaced with ONE <br />.
I hope my question is clear to you, sorry for the weird English
This should work:
preg_replace("/[\n\r]+/", "<br />", // rest of your code
=====================^ add a + there on your regex
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);
I'm parsing some messy HTML code with PHP in which there are some redundant tags and I would like to clean them up a bit. For instance:
<br>
<br /><br />
<br>
How would I replace something like that with this using preg_replace()?:
<br /><br />
Newlines, spaces, and the differences between <br>, <br/>, and <br /> would all have to be accounted for.
Edit: Basically I'd like to replace every instance of three or more successive breaks with just two.
Here is something you can use. The first line finds whenever there is 2 or more <br> tags (with whitespace between and different types) and replace them with wellformated <br /><br />.
I also included the second line to clean up the rest of the <br> tags if you want that too.
function clean($txt)
{
$txt=preg_replace("{(<br[\\s]*(>|\/>)\s*){2,}}i", "<br /><br />", $txt);
$txt=preg_replace("{(<br[\\s]*(>|\/>)\s*)}i", "<br />", $txt);
return $txt;
}
This should work, using minimum specifier:
preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $multibreaks);
Should match appalling <br><br /><br/><br> constructions too.
this will replace all breaks ... even if they're in uppercase:
preg_replace('/<br[^>]*>/i', '', $string);
Try with:
preg_replace('/<br\s*\/?>/', '', $inputString);
Use str_replace, its much better for simple replacement, and you can also pass an array instead of a single search value.
$newcode = str_replace("<br>", "", $messycode);