Jump to content

Recommended Posts

Suppose you have a form with a <textarea>.  When you submit the form, the php will assign the value of whatever is stored in the textarea to a variable.  How do you detect where the user had pressed the enter/return key at?

I'd like to be able to str_replace() the newlines/carriage returns in the string with html <br />'s but can't seem to determine how to detect their presence.

I currently am trying the following, and have tried all the variations of the first value \n, \n\r, \r, \r\n, but no luck in getting it detected and replaced.
[code]
$newpostbody = str_replace('\n', '<br />', $newpostbody);
[/code]
Link to comment
https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/
Share on other sites

This is common in PHP. There's actually a special function for it! nl2br().

http://us3.php.net/manual/en/function.nl2br.php

All you have to do is feed the string from the textarea to it and it'll do the rest (only works for newlines '\n' though.)

[code]$htmlized_text = nl2br($_POST['textarea']);[/code]
Ah, very interesting.  I was getting desperate, and found that if I use double quotes instead of singles, it worked fine.  I did have to add the \r in order to get it to save to the database correctly, though, but all is well.

solution:
[code]
$newpostbody = str_replace("\r\n", "<br />", $newpostbody);
[/code]
Alas, it does not remove the \r as well.

[code]
$newpostbody = nl2br($newpostbody);
[/code]

I suppose I may have to stick with my longer str_replace.

And the reason I'm running it on the string and not directly on the $_POST is because I have several other replacement filters like to change <tab> into several nbsp's and such.
[quote author=c4onastick link=topic=119478.msg489419#msg489419 date=1166680707]If that's what your data looks like and nl2br doesn't work give this a shot:
[code]$htmized_text = preg_replace('/(\r\n)/', '<br \>\1', $source);[/code]
[/quote]

that preg_replace still added a line break in the saved data.  The output on-screen looks fine, but i need it converted to a single line when I place it in the database.
[quote author=thorpe link=topic=119478.msg489435#msg489435 date=1166682271]
You really should avoid storing html in the database whenever possible. What if (in the future) you want to display the data in another client besides a browser?
[/quote]How would you change the text? Would you recommend writing something to translate the text into the requested display format? (I'm curious, I've got a couple databases full of limited html, never even though twice about it.)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.