ionis Posted February 15, 2011 Share Posted February 15, 2011 Hey guy, I really need your help with this. I have Input from a text area, in this format: a b c d e f g 12 34 435 124 What I need do to is to output only a certain line from every block of text, every block is divided by one or more empty lines. I have done this: $input = $_POST["input"]; $lines = explode("\r",$input); $num = count($lines); $line_to_extract = $_POST['line_to_extract']; for($i=0;$i<$num;$i++) { if($i == $line_to_extract) { echo 'Line '.$i.': '.$lines[$i].'<br />'; } } What I need to do is to detect the empty line so I can keep track of the line to extract. PLEASE HELP!!! Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted February 16, 2011 Share Posted February 16, 2011 Use 2 counters: $i for the entire array and $j for the line no within a block, as follows: for($i=0,$j=1; $i<$num; $i++,$j++) { if (strlen(trim($lines[$i])) == 0) { $j=0; continue; } if($j == $line_to_extract) { echo 'Line '.$i.': '.$lines[$i].'<br />'; } } Ronald Quote Link to comment 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.