Jump to content


Photo

Row changes in textare


  • Please log in to reply
15 replies to this topic

#1 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 27 July 2006 - 04:24 PM

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:
<br>

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

Plz help... ;D
// XeroXer

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 July 2006 - 04:27 PM

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

#3 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 27 July 2006 - 04:36 PM

Well thanks. That added the
<br>
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
<br>
after them.
So that helped in someway but I still need the linebreaker that comes with 'enter' removed...

EDIT:
I also need it to add a
<br>
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?
// XeroXer

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 July 2006 - 04:38 PM

Do somtyhing like this:
$text = str_replace(array("\r", "\n"), '', $text);
That'll remove the newlines. Make sure you use this after nl2br, otherwise nl2br wont work.

#5 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 27 July 2006 - 04:41 PM

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?
// XeroXer

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 July 2006 - 04:47 PM

You'll want to use wordwarp:
<?php

$text = "ttttttttttttttttttthhhhhhhhhhhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiiiissssssssssssssssssssssss aaaaaaaaaaaaaaaaa lllllllllllllllllllllllllllllllllllllllllllloooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggggg pppppppppppppppppppppppppppppppppppppppppppppppppppoooooooooooooooooooooooooooooooooooooooooooooooooooooooosssssssssssssssssssssssssssssssssssssssssssssssssssssstttttttttttttt.";

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

echo $newtext;
?>


#7 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 27 July 2006 - 05:36 PM

Thank you once again.
Now my guestbook works like a charm... :-)
// XeroXer

#8 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 28 July 2006 - 11:07 AM

NEW QUESTION!

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.
1) How do i strip of the html code before entering it into the file?
2) How do I print the file with the code intact but not working?
3) Witch one of these do you think I should use?
// XeroXer

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 July 2006 - 11:17 AM

To strip any html tags use the function called strip_tags, also use other functions called htmlspecialchars, htmlentities and addslashes.

#10 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 28 July 2006 - 11:39 AM

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. :-(
// XeroXer

#11 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 July 2006 - 11:55 AM

We have the php manual on the main site, phpfreaks.com

Here are links to those functions:
htmlentities
htmlspecialchars
strip_tags

#12 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 28 July 2006 - 12:03 PM

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?
// XeroXer

#13 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 July 2006 - 12:06 PM

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;

#14 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 28 July 2006 - 12:10 PM

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:
<?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);
?>

And for name, email and homepage it's this:
<?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);
?>

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?
// XeroXer

#15 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 July 2006 - 02:09 PM

Rather than doing each function seperatly, I'd do it all in one line.
//Set name
$name = htmlentities(strip_tags($_POST['name']));
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.

#16 XeroXer

XeroXer
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts
  • LocationSweden

Posted 28 July 2006 - 03:47 PM

Thanks!
My rows now look like this:
<?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);
?>

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 :-)
// XeroXer




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users