Jump to content

[SOLVED] Split input by lines?


sam06

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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