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
Related
I'm using the explode function in tandem with the implode to take a CSV text and make it display clearly on screen, like so:
$CSV_STRING = 'Quarterly update,,
Question Topic,Question Title,Question Text
Question Topic,Question Title,Question Text';
----------------------
$array = explode(PHP_EOL, $CSV_STRING);
$arrayImploded = implode("<br>", $array);
$arrayExploded = explode(",", $arrayImploded);
$arrayFinal = implode("<br>", $arrayExploded);
echo $arrayFinal;
My issue here is that I'm trying to detect the new line with PHP_EOL and it doesn't seem to detect it. I've used \n and \n\r to similar results.
What should I use to correctly display my results like so:
Quarterly update
Question Topic
Question Title
Question Text
Question Topic
Question Title
Question Text
Thank you!
Rather than exploding/imploding, you should be able to run it through nl2br() which will convert the new lines to <br /> tags and then if you just use str_replace() to replace the commas with <br /> tags as well, it should display as you want...
$CSV_STRING = 'Quarterly update,,
Question Topic,Question Title,Question Text
Question Topic,Question Title,Question Text';
$out = nl2br($CSV_STRING);
$out = str_replace("<br />", "<br /><br />", $out);
echo str_replace(",", "<br>", $out);
To create an extra break between the blocks, this just adds another <br /> tag to each one just added.
Note that I've used <br> rather than <br /> so you should see which part of the code is changing which part of the original string. But use <br /> for 'correctness'.
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
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>
I have this message (without the quotes, it's just for being precise):
"hey, here i am<br /><br />
"
Note the white space after the line break. So here's the thing: I'm trying to remove all the invisible chars and the <br /> of the message, all of them at the end of the message, with a regex to have something like "hey, here I am". But I must do something wrong because I can't make it work. That's what I tried:
$content = preg_replace('{(<br(\s*/)?>| |\r\n|\r|\n| )+$}i', '', $content);
But the message remains the same at the end. Must be something simple I missed. Thank you for your help!
You don't need a regular expression to do that. Use the strip tags function to remove the tags.
$str = 'hey, here i am<br /><br />';
echo strip_tags($str);//yields hey, here i am
Don't try to write your own regular expressions to parse HTML when you have tools that already do it. Sometimes it's necessary depending on case, but in your case I would say it isn't. Just use the built in function.
You can use the following regex:
([^\s\w",](?:br\W*\s*)+)"$
Working demo
The code is:
$re = "/([^\\s\\w\\",](?:br\\W*\\s*)+)\\"$/";
$str = "\"hey, here i am<br /> test<br /><br />\n \"";
$subst = '';
$result = preg_replace($re, $subst, $str);
You should not use regular expression to do what you wanted. Take a look at this answer: RegEx match open tags except XHTML self-contained tags
Instead use strip_tags().
$str = 'hey, here i am<br /><br />';
$str=~s{(<br />|\s)*$}{}ig;
use this code this might help you
my $cnt;
$cnt = "hey, here i am<br /><br />";
$cnt =~s/(<br \/>)*//isg;
print $cnt;
output : "hey, here i am"
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);