Jump to content

[SOLVED] Split input by lines?


sam06

Recommended Posts

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?

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!

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 />";

}

?>

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

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.