Jump to content

Help detecting empty lines In a text area


ionis

Recommended Posts

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!!!

 

 

 

 

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

Archived

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

×
×
  • Create New...

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.