Jump to content


Photo

Textarea Split


  • Please log in to reply
5 replies to this topic

#1 Baving

Baving
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 15 September 2006 - 02:39 PM

I am trying to enter information into a textarea like: -

Hello
No
You

And then split it so that each word can be inserted into a new row in a database.

Any ideas on how to do this effectivley?

Thanks

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 15 September 2006 - 02:53 PM

<pre>
<?php

	$string = "Hello\nNo\nYou";
	print_r(explode("\n", $string));

?>
</pre>


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 wildteen88

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

Posted 15 September 2006 - 04:46 PM

Yes you'll want to use explode, like effigy has shown, however if you are dealling with user input from a form field not all OSs use the same whitespace character for newlines.

\r\n - Windows
\r - Mac
\n - Linux

So if you are dealing with user input from a textarea in a form, you'll wnat to do something like this:
// Convert other OS newlines into the linux newline char
$words = str_replace(array("\r\n", "\r"), "\n", $_POST['words_txtbox']);

$word = explode("\n", $words);


#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 15 September 2006 - 05:27 PM

Thanks for the catch; however, textarea's are not platform-specific. See application/x-www-form-urlencoded. When dealing with form data:

Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 September 2006 - 05:53 PM

Thanks for the catch; however, textarea's are not platform-specific. See application/x-www-form-urlencoded. When dealing with form data:

Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').


Then it should be
<?php

	$string = "Hello\nNo\nYou";
	print_r(explode("\r\n", $string));

?>


#6 Baving

Baving
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 15 September 2006 - 08:27 PM

Thanks everyone :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users