I'm having some problems with a "bb parser" I'm coding. Or, well, not with the parser itself, but the nl2br modifying it.
The string from the database is like the following:
text text text
[code]code code code[/code]
text text text
Now, nl2br puts one br / after the first "text text text", and then another one below that, so there's two line breaks before the [code] tag (which actually is correct, but not what I want).
Is there any way I can limit how many br's are entered in a row? I can't seem to find a solution that's simple enough.
Thanks in advance, guys.
In addition to previous solution, I add a different one, since Fredrik asked for it. This will replace double <br> after nl2br instead of before.
$string = nl2br( $string );
$string = preg_replace( '/(<br(?: \\/)?>\\r?\\n?\\r?)(?=\\1)/is', '', $string );
You could for example replace two linebreaks (or more) by one by using preg_replace :-)
You could use
$string = str_replace(array("\r\n\r\n", "\n\r\n\r", "\n\n", "\r\r"), array("\r\n","\n\r","\n","\r"), $string);
This prevents double <br> tags. Preg_replace as suggested before is better if there could be more than two new lines in a row.
Related
I'm making a CSV file, one of the database filed has stored a section called comments/note which obviously have some commas and line breaks in it too. Looked around the web found usage of preg_replace(), not much familiar with regular expressions there fore combined two different ones and not getting anything in result its totally blank and i know all records have some sort of comments in it
this i used
preg_replace( "/\r|\n/|/[,]/", "", $string )
Please what do I need to do here get one text back without line breaks and commas
Regards
You can do it using strip_tags() and preg_replace();
$clean_str = trim(preg_replace('/\s\s+/', '', strip_tags($string)));
or try this
$clean_str = str_replace(array("\r\n","\r","\n", ","), '', strip_tags($string));
I have a jquery editable div that when you click on it you can edit the text. The problem is that when the data is called from the db and placed into the paragraph I keep getting a \n for every space. How can I replace the \n with an actual new line.
I tried nl2br(), but that's not very convenient for my users since they then have to play with the <br /> when they want to edit the paragraph.
Any thoughts?
What about:
str_replace("\\n", "", $str); // see if this gets rid of them
Then this should work to put actual newlines in there:
str_replace("\\n", "\n", $str); // should replace with actual newline
try:-
$strippedText = str_replace(chr(10), '', $textFromDB);
or
$strippedText = str_replace(chr(10), '<br/>', $textFromDB);
Does this work? (Working on the possiblity that the newlines are already escaped).
$strippedText = str_replace('\\n', ' ', $textFromDB);
Are you using a ready made solution or making your own? I use http://aloha-editor.org/ for stuff like this and it's mostly problem free.
Have you tried str_replace?
$myTextFromDB = str_replace('\n', PHP_EOL, $myTextFromDB);
Ok, I think this is different enough that I should do a separate answer for it.
Are you saying a literal "slash n" shows up on the page? Or are you saying that your newlines show up as spaces?
If it's the latter, then there's no way around that. HTML will show newlines only as a space - you have to convert to br tags to break the line if it's not in a textarea context. But you can always convert them back to newlines when you pop that textarea up for your user and this should work well for people.
Currently, I'm using the following code:
preg_replace('/\s+/m','<br>',$var);
to replace line ends with <br> elements. An example of what I want:
Text Text Text
Text Text Text
Text Text Text
Should end up being:
Text Text Text<br>Text Text Text<br><br>Text Text Text
This does what it needs to, but I'd like to recognize when there is a double space and add two breaklines, instead of a single one. How can I do this, while retaining the current effect for single break lines?
I'm not too familiar with how preg_replace() works and I actually had to get help here to get that function in the first place. I took a look in the PHP manual and the function seemed a little confusing. Would anyone know of a site where I could learn how it works correctly?
You can simply replace each end-of-line character with <br />:
$var = str_replace(array("\r\n", "\n"), '<br />', $var);
There is no need to use a regular expression, but if you really want, you can use preg_replace to achieve the same effect:
$var = preg_replace("/\r?\n/", '<br />', $var);
you can do this by adding the g-modifier to the preg_replace like so:
preg_replace('/\s+/mg','<br>',$var);
preg stands for Perl Regular Expression - you'll find a lot more examples with this search string, e.g. this site or this site (I'm actually unsure, what the m-modifier does?)
Alternatively, you could use the simple $var = str_replace(' ', '<br', $var). I'm unsure, which one is faster.
Edit: If you want to replace newlines with html-breaks, use the nl2br() function.
php has a built in function for this
echo nl2br( $var );
this does \n, \r\n, \r, and \n\r
http://www.php.net/manual/en/function.nl2br.php
Check out this one. Not sure you are asking for that, but at least it works for me
$input = <<<DOC
Test Test Test
Test Test Test
Test Test Test
DOC;
$output = preg_replace("/$/m","<br/>",$input);
echo $output;
Hovewer, nl2br does just the same.
Is this what you’re trying to do?
<?php
$text = <<<EndText
Text Text Text
Text Text Text
Text Text Text
EndText;
$text = str_replace("\r", "", $text);
$text = str_replace("\n", "<br>", $text);
echo $text;
?>
I know about the nl2br function... But is there a way to set a maximum length of a line?
Im using php to get results from a form, which contains a textarea.
I dont want words to get broken to newlines, like the word 'example' to become 'example'
Thanks
You can use wordwrap to force a line break:
wordwrap($str, 123, "<br>\n", true)
You might be interested in the wordwrap function in php.
I have tried and tried and tried now.
I have a mysql field (TEXT) which contains the value of a textarea.
That is, if the text is written EXACTLY like this:
Hello. Hello.
Hello.
Hel
lo.
Then thats how it will appear in the mysql field because Im using wordwrap and nl2br functions when inserting it there.
Now, I am creating a dynamic page, where the 'meta description content' is updated with the 'TEXT' content, BUT, in one long string without any breaks and new lines.
That said, I need to make text to be in one string.
I have used this:
str_replace ("<br/>", "", $string);
This displays the text as one string, yes, but when viewing the source code of the page, you can see that the breaks are there, like this:
<meta name="description" content="
Hello. Hello.
Hello.
Hel
lo." />
I have also tried replacing the with '\n' and others, without success...
How can I solve this irritating problem?
Let me know if you need more input, and I will update this Q.
Replace all groups of whitespace with a single space:
$output = preg_replace('!\s+!', ' ', $input);
Try replacing "\n" with "" instead.
handy function:
function removeEmptyLines($s) {
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $s);
}
You might try to something like this str_replace("\n\r", "", $string); I have found that sometimes the return character \r is hiding in there someplace.