remove \n from paragraph - php

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.

Related

Replace newlines within <ul>-tag

I have been battling with this for a while, because I guess I am not that awesome at regex. I have a few ul-tags and within those I would like to replace every /n newline with "" (nothing).
So, I would like to have this
other lines right here
[ul]
[li]line[/li]
[li]line 1[/li]
[li]line 2[/li]
[/ul]
other lines right here
[ul]
[li]line[/li]
[li]line 1[/li]
[li]line 2[/li]
[/ul]
other lines right here
changed to
other lines right here
[ul][li]line[/li][li]line 1[/li][li]line 2[/li][/ul]
other lines right here
[ul][li]line[/li][li]line 1[/li][li]line 2[/li][/ul]
other lines right here
I only want to get rid of the newlines within the ul-tag. How do I go about this issue I have?
Should I pre_match_all every ul tag and then format the matched content the way I would like it to be? Or is there any easier solution with preg_replace that takes care of all the newlines within the tag?
Thanks for the help already!
I'd look at it a different way. the [li]s will always be inside a [ul] (well, hopefully!). So you can just match newlines either side of those:
$string = preg_replace('/\R*(\[\/?li\])\R*/', '$1', $string);
You could do the same with a couple of str_replaces:
$string = str_replace("\r\n[li]", "[li]", $string);
$string = str_replace("[/li]\r\n", "[/li]", $string);

Limit amount of br's nl2br shows in a row?

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.

PHP str_replace not working correctly

I'm using str_replace and it's not working correctly.
I have a text area, which input is sent with a form. When the data is received by the server, I want to change the new lines to ",".
$teams = $_GET["teams"];
$teams = str_replace("\n",",",$teams);
echo $teams;
Strangely, I receive the following result
Chelsea
,real
,Barcelona
instead of Chealsea,real,Barcelona.
What's wrong?
To expand on Waage's response, you could use an array to replace both sets of characters
$teams = str_replace(array("\r\n", "\n"),",",$teams);
echo $teams;
This should handle both items properly, as a single \n is valid and would not get caught if you were just replacing \r\n
Try replacing "\r\n" instead of just "\n"
I would trim the text and replace all consecutive CR/LF characters with a comma:
$text = preg_replace('/[\r\n]+/', ',', trim($text))
I had the same issue but found a different answer so thought I would share in case it helps someone.
The problem I had was that I wanted to replace \n with <br/> for printing in HTML. The simple change I had to make was to escape the backslash in str_replace("\n","<br>",($text)) like this:
str_replace("\\n","<br>",($text))

Replacing text function in php

I want to clean up some parsed text such as
\n the said \r\n\r\n\r\n I look in your eyes my dear\r\n\r\nI see green rolling Forests\r\n\r\nI see the far away Sky\r\n\r\nThey turn into the rain\r\n\r\n\r\nI see high soaring eagles... more\n
So I want to get rid of the "\n", "\r\n", "\r\n\r\n", "\r\n\r\n\r\n", "\r\n\r\n\r\n\r\n" and "\r". That's all the combinations that appear in my parsed text.
Is there a way to do this in php?
If you just want 1 newline instead of multiple I would suggest this:
$clean = preg_replace(array("/\r+/","/(\n){2,}/"),array("","\n"),$text);
Otherwise str_replace to strip out newlines or nl2br will do the job. You could also adapt the regex to replace 1 or more newlines with a BR tag:
$clean = preg_replace(array("/\r+/","/\n+/"),array("","<br />"),$text);
What about
$text = str_replace(array("\n", "\r"), '', $text);
That will remove all new line characters.
If you want them as new lines, I'd change the replace to <br /> for HTML (or better still, use PHP's nl2br()), or standardise them in normal text with \n, for example.

How can I make this string from a 'textarea' to be on one single line?

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.

Categories