i'm getting a string from an mssql -database to my .php-page.
For good look, I want to replace the newlines with <br />
so I tried the following (one at a time):
echo nl2br($data);
echo str_replace(chr(10), "<br />",str_replace(chr(13), "<br />", $data))
echo str_replace("\n", "<br />",str_replace("\r", "<br />", $data))
The HTML-source code looks all right:
blablalba<br />sdsddsfdfs<br />fds<br />dfsdfs<br />fdsdsf<br />:_k,ölmjlö<br />öä.löälöä#<br />
But the result on the HTML is empty, and Chrome developer-tools is displaying following:
<br><br><br><br><br><br><br>
What am I missing?
echo $data is giving me the right result but without <br />'s
blablalba sdsddsfdfs fds dfsdfs fdsdsf :_k,ölmjlö öä.löälöä#
Regards
Use Wordwrap...
Wrap a string into new lines when it reaches a specific length:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>
Result:
An example of a
long word is:
Supercalifragulistic
Related
I'm looking to literally display the contents of echo or print instead of PHP processing the HTML contained within.
For example, if I have the following:
echo("<a href='$file'>$file</a> <br />\n"); the PHP parser will literally display all my files within a directory as links. What if I wanted to literally output the HTML tags without executing them so that would be displayed as plain text?
Thanks.
Instead of echo, use php function htmlentities inside a html code tag:
echo "<code>";
echo htmlentities("<a href='$file'>$file</a> <br />\n");
echo "</code>";
'<' = <
'>' = >
echo ("<a href='$file'>$file</a> <br />\n");
or...
echo htmlspecialchars("<a href='$file'>$file</a> <br />\n");
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>
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 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);