Jump to content

Archived

This topic is now archived and is closed to further replies.

XeroXer

Row changes in textare

Recommended Posts

Hi there...

This is my problem:
I am making a guestbook that uses a flat file.
Just a simple one for my mother-in-law's homepage.
The problem I am having is that when people enter a two rowed message the file bugs out.
Well they can enter a two-row message if they just type and type.
But as quickly as they press enter to change the row it doesn't work.
They can still post the message but in the textfile that just makes a new line and I would like it to change it to:[code]<br>[/code]

So what I would like to know is if I can get a normal 'enter' in a textarea to become a [code]<br>[/code] in the textfile?

Plz help... ;D

Share this post


Link to post
Share on other sites
You'll want to use nl2br, it converts newlines into html line breaks. The newlines are there still but the browser ignores them

Share this post


Link to post
Share on other sites
Well thanks. That added the [code]<br>[/code] that I wanted.
Though it did not remove the line added.
The message still winds up in multiple rows in the textfile but now with [code]<br>[/code] after them.
So that helped in someway but I still need the linebreaker that comes with 'enter' removed...

[b]EDIT:[/b]
I also need it to add a [code]<br>[/code] like a automatic linebreaker.
If a person just types a loooooong message without any space the site expands.
Is it possible to make it do automatic linebreaker if no space is found?

Share this post


Link to post
Share on other sites
Do somtyhing like this:
[code]$text = str_replace(array("\r", "\n"), '', $text);[/code]
That'll remove the newlines. Make sure you use this after nl2br, otherwise nl2br wont work.

Share this post


Link to post
Share on other sites
OMG that did the trick.
Now they can press enter how many times they want.
The message will still be presented the right way...

Though the problem I edited in the other message can that be solved?

Share this post


Link to post
Share on other sites
You'll want to use wordwarp:
[code]<?php

$text = "ttttttttttttttttttthhhhhhhhhhhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiiiissssssssssssssssssssssss aaaaaaaaaaaaaaaaa lllllllllllllllllllllllllllllllllllllllllllloooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggggg pppppppppppppppppppppppppppppppppppppppppppppppppppoooooooooooooooooooooooooooooooooooooooooooooooooooooooosssssssssssssssssssssssssssssssssssssssssssssssssssssstttttttttttttt.";

$newtext = wordwrap($text, 200, "<br />", true);

echo $newtext;
?>[/code]

Share this post


Link to post
Share on other sites
Thank you once again.
Now my guestbook works like a charm... :-)

Share this post


Link to post
Share on other sites
[size=12pt][u][color=red][b]NEW QUESTION![/b][/color][/u][/size]

Now I have another question still regarding my guestbook.
Thought I'd post it here instead of creating a new thread.

Now my flatfile guestbooks work almost as I want it.
I have gotten control over the length of the text entered.
I also got so it supports pressing 'enter' while writing a message.
Now I need it to remove the html code and anything similar. :-)

I want to strip the html code from the message before entering it into the textfile.
Or just print it out plain html and not working.
Because now people can enter what they like and ruin my guestbook.
A javascript that makes something annoying or a few rows of html that changes everything.

So please help me.
[b]1)[/b] How do i strip of the html code before entering it into the file?
[b]2)[/b] How do I print the file with the code intact but not working?
[b]3)[/b] Witch one of these do you think I should use?

Share this post


Link to post
Share on other sites
To strip any html tags use the function called strip_tags, also use other functions called htmlspecialchars, htmlentities and addslashes.

Share this post


Link to post
Share on other sites
I have to use them all?
I was going to check the quickref on php.net but the site is down as it seems.
So I need the info on the functions. :-(

Share this post


Link to post
Share on other sites
We have the php manual on the main site, phpfreaks.com

Here are links to those functions:
[url=http://www.phpfreaks.com/phpmanual/page/function.htmlentities.html]htmlentities[/url]
[url=http://www.phpfreaks.com/phpmanual/page/function.htmlspecialchars.html]htmlspecialchars[/url]
[url=http://www.phpfreaks.com/phpmanual/page/function.strip-tags.html]strip_tags[/url]

Share this post


Link to post
Share on other sites
If I use htmlentities and strip_tags on all fields will they still work?

Name, Email, Homepage and Message.
Those fields are in my guestbook.
If I use both those functions on all those fields will still long homepage and mail adresses work?

Share this post


Link to post
Share on other sites
Yes. As those function will stripout any html from those fields, and htmlentities will convert some hmtls characters into their HTML equivalent, for exampe an opeing triangle barcket < will be converted &lt;

Share this post


Link to post
Share on other sites
Just one last thing... :-)
Is there any special order these should be taken in?
Or can I make somethings do more on one row?

This is my code for the message field right now:
[code]<?php
//Set message
$message = $_POST["message"];
$message = strip_tags($message);
$message = htmlentities($message, ENT_QUOTES);
$message = nl2br("$message");
$message = str_replace(array("\r", "\n"), '', $message);
$message = wordwrap($message, 50, "<br />", true);
?>[/code]

And for name, email and homepage it's this:
[code]<?php
//Set name
$name = $_POST["name"];
$name = strip_tags($name);
$name = htmlentities($name, ENT_QUOTES);
$name = wordwrap($name, 50, "<br />", true);
//Set email
$email = $_POST["email"];
$email = strip_tags($email);
$email = htmlentities($email, ENT_QUOTES);
$email = wordwrap($email, 50, "<br />", true);
//Set homepage
$homepage = $_POST["homepage"];
$homepage = strip_tags($homepage);
$homepage = htmlentities($homepage, ENT_QUOTES);
$homepage = wordwrap($homepage, 50, "<br />", true);
?>[/code]

Does that look good or not?


The message field is the only one that is a textarea. The other ones are just a "textrow".
That is why I don't use the nl2br and str_replace with name, email or homepage
Is that correct?

Share this post


Link to post
Share on other sites
Rather than doing each function seperatly, I'd do it all in one line.
[code]//Set name
$name = htmlentities(strip_tags($_POST['name']));[/code]
Also for each text field, I'd add the maxlength attribute to set the maxumin number of characters for each field. Then you dont need to use wordwrap.

Share this post


Link to post
Share on other sites
Thanks!
My rows now look like this:
[code]<?php
//Set name
$name = htmlentities(strip_tags($_POST['name']));
//Set email
$email = htmlentities(strip_tags($_POST['email']));
//Set homepage
$homepage = htmlentities(strip_tags($_POST['homepage']));
//Set message
$message = nl2br(htmlentities(strip_tags($_POST['message'])));
$message = str_replace(array("\r", "\n"), '', $message);
$message = wordwrap($message, 50, "<br />", true);
?>[/code]

That looks a lot better and I also added the lengthlimit on the textfields.
Thanks for all the help.

Will be back for more questions regarding other stuff :-)

Share this post


Link to post
Share on other sites

×

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.