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 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 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.. 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. 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? 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! 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; } ?> 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 />"; } ?> 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... 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 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); 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. 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
Archived
This topic is now archived and is closed to further replies.