Jump to content
xisto Community
Sign in to follow this  
nightfox1405241487

Converting <enter> To \n In Textbox

Recommended Posts

I'm making a script that emails data and in the message box, I need all <enter> to be converted to \n so when it is mailed, it will format properly into:Thisis atestWhen submitted, it would be turned into:This \n is \n a \n testSo that it can easily be mailed. Or does PHP do this automatically? Thanks![N]F

Share this post


Link to post
Share on other sites

Lets say your textarea box in a form where the message is entered is called message then in you form processing page if you are wanting to e-mail what was entered in the textarea so you would access the data in the textarea with $_POST['message'] and to use your message variable first make a short variable out of it like $message = $_POST['message']; then for the mail() function you would use wordwrap tomake sure that there are no more than 72 characters per line with the below code.

$message = wordwrap($message, 72, "<br>\n');
This will allow you to send the mail with no errors, by default mail will be sent in plain text but if you enter several returns or line breaks they will show up on plain text through the mail, or you would need to use <br /> tags (in the textarea), you would also have to format the message with the HTML tags you want to use if you set up your extra headers to send the mail with HTML. Edited by Houdini (see edit history)

Share this post


Link to post
Share on other sites

Well, I don't know if you need it, but if you don't want to send html emails from your web server, you also would need to use strip_tags() function -> http://php.net/strip_tags - it will strip all html and php tags from the function, then you could use wordwrap. Or if the server has magic quotes on, you also would need to make stripslashes() so maybe you could use this code:

 

<?phpif (isset($_POST['message']) ) {	$message = wordwrap(strip_tags(stripslashes($_POST['message']) ), 72, "\n");	if (FALSE !== mail(/* Your Parameters */) ) {		echo 'Mail send with Success' . "\n";	} else {		echo 'Error: Could not send Mail' . "\n";	}}?>

Of course, sometimes you want the HTML code to be in the mail, so you can just remove the strip_tags() function. :P

Edited by Quatrux (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.