sam06 Posted July 11, 2009 Share Posted July 11, 2009 Hopefully this won't take long! ; I'd like to take an input of multiple lines, split it by line, and perform an action with it. e.g. Input: 1000 1001 1002 1003 And it would do something with 1000, then 1001, then 1002 etc. Any ideas? Many thanks, Sam Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/ Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 $input = explode("\n",$input); edit: if this input is a text file you are reading, you can use file Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-873555 Share on other sites More sharing options...
Alex Posted July 11, 2009 Share Posted July 11, 2009 $split = explode("\n", $input); Then the first line would be $split[0], second would be $split[1], etc.. Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-873556 Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 $split = explode("\n", $input); Then the first line would be $split[0], second would be $split[1], etc.. yes. Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-873557 Share on other sites More sharing options...
sam06 Posted July 14, 2009 Author Share Posted July 14, 2009 Many thanks; I'm not too sure how to emplement this though...? I've tried: <?php $input = $_POST['input']; $count = 0; $while = 0; $split = explode("\n", $input); while($while == 0) { if ( $split[$count] == "" ) { $while = 1 ; } else { // my action will go here echo $count; echo ' - '; echo $split[$count]; echo '<br>'; // $count = $count + 1; } } ?> It's working, but coming up with: Notice: Undefined offset: 2 in C:\wamp\www\splitfile.php on line 11 Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875132 Share on other sites More sharing options...
Adam Posted July 14, 2009 Share Posted July 14, 2009 You can just use something like: $input = $_POST['input']; $split = explode("\n", $input); foreach ($split as $line) { if ($line != '') { echo $line . '<br />'; } } You'll probably want to add a <br /> tag to the end otherwise they'll all be on the same line. Edit: Just seen the <br> you had before! Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875134 Share on other sites More sharing options...
nbarone Posted July 14, 2009 Share Posted July 14, 2009 <?php $input = $_POST['input']; $split = explode("\n", $input); foreach($split as $k){ echo $k; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875135 Share on other sites More sharing options...
phporcaffeine Posted July 14, 2009 Share Posted July 14, 2009 I think you may be making it more complicated than it need be ... <?php $input = $_POST['input']; $split = explode("\n", $input); foreach ($split as $key => $value) { //DO ACTION ON THE INDIVIDUAL SPLIT INDEX echo $value . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875138 Share on other sites More sharing options...
Adam Posted July 14, 2009 Share Posted July 14, 2009 I think you may be making it more complicated than it need be ... <?php $input = $_POST['input']; $split = explode("\n", $input); $key = 0; $limit = count($split); while($limit <= 0) { //DO ACTION ON THE INDIVIDUAL SPLIT INDEX echo $split[$key]; //INCREMENT FOR THE NEXT ITERATION $key++; } ?> I think you are... Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875140 Share on other sites More sharing options...
sam06 Posted July 14, 2009 Author Share Posted July 14, 2009 Thanks a lot, I changed it a bit: $tocheck = isset($split[$count]) ? $split[$count] : ''; if($tocheck == ""){ Really appreciate it, Sam Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875141 Share on other sites More sharing options...
nbarone Posted July 14, 2009 Share Posted July 14, 2009 Thanks a lot, I changed it a bit: $tocheck = isset($split[$count]) ? $split[$count] : ''; if($tocheck == ""){ Really appreciate it, Sam is this to find the length of the split? if so you can use sizeof($array); Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875143 Share on other sites More sharing options...
Adam Posted July 14, 2009 Share Posted July 14, 2009 No he's checking if $split[$count] is set, and then doing another if to check if the previous if was successful- bizarre! You could just use a foreach () loop and make things much easier. Quote Link to comment https://forums.phpfreaks.com/topic/165613-solved-split-input-by-lines/#findComment-875146 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.