Jump to content

Archived

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

Baving

Textarea Split

Recommended Posts

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

Share this post


Link to post
Share on other sites
[code]
<pre>
<?php

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

?>
</pre>

[/code]

Share this post


Link to post
Share on other sites
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:
[code=php:0]// Convert other OS newlines into the linux newline char
$words = str_replace(array("\r\n", "\r"), "\n", $_POST['words_txtbox']);

$word = explode("\n", $words);[/code]

Share this post


Link to post
Share on other sites
Thanks for the catch; however, textarea's are not platform-specific. See [url=http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.13.4]application/x-www-form-urlencoded[/url]. When dealing with form data:

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

Share this post


Link to post
Share on other sites
[quote author=effigy link=topic=108180.msg435025#msg435025 date=1158341226]
Thanks for the catch; however, textarea's are not platform-specific. See [url=http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.13.4]application/x-www-form-urlencoded[/url]. When dealing with form data:

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

Then it should be [code]<?php

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

?>[/code]

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.