Baving Posted September 15, 2006 Share Posted September 15, 2006 I am trying to enter information into a textarea like: -HelloNoYouAnd 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 Quote Link to comment https://forums.phpfreaks.com/topic/20869-textarea-split/ Share on other sites More sharing options...
effigy Posted September 15, 2006 Share Posted September 15, 2006 [code]<pre><?php $string = "Hello\nNo\nYou"; print_r(explode("\n", $string));?></pre>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20869-textarea-split/#findComment-92439 Share on other sites More sharing options...
wildteen88 Posted September 15, 2006 Share Posted September 15, 2006 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 - LinuxSo 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] Quote Link to comment https://forums.phpfreaks.com/topic/20869-textarea-split/#findComment-92518 Share on other sites More sharing options...
effigy Posted September 15, 2006 Share Posted September 15, 2006 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 Link to comment https://forums.phpfreaks.com/topic/20869-textarea-split/#findComment-92542 Share on other sites More sharing options...
Daniel0 Posted September 15, 2006 Share Posted September 15, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/20869-textarea-split/#findComment-92563 Share on other sites More sharing options...
Baving Posted September 15, 2006 Author Share Posted September 15, 2006 Thanks everyone :) Quote Link to comment https://forums.phpfreaks.com/topic/20869-textarea-split/#findComment-92644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.