0

I've been trying to create a tracker and everything is going smoothly thus far, just one new feature I'm trying to work on has me stumped. I'm trying to have it so the person who starts a tracker can enter names and the tracker will grab the names the user entered and look them up.

The only way that I can think of accomplishing this would be to insert the names from $_POST['names'] and insert them into an array, but when I do this, it only creates one index inside the array.

This is the code that I'm using:

$lol = array();

$l = array($_POST['names']);

foreach ($l as $textarea) {
    array_push($lol, nl2br($textarea));
}

for ($a = 0; $a < count($lol); $a++) {
    echo "index " . $a . ": " . $lol[$a];
}

The result of that is:

index 0: lol
not
sure
how
this
will
work

I typed the above out in the text field with spaces. Here's the HTML file that I'm using as well:

<body>

    <form name="form" action="test.php" method="GET">
        <div align="center">
            <textarea cols="13" rows="10" name="names"></textarea><br>
            <input type="submit" value="lol">
        </div>
    </form>

</body>

So I guess what I'm asking is, is it possible to have a person put, say, "Ron", "Mark", "Tommi", and "Daniel" in the text box, could I insert "Ron", "Mark", "Tommi" and "Daniel" into an array so that their index would be [0, 1, 2, 3] respectively, and if so how would I achieve this?

1 Answer 1

4

Use explode to split a string by another substring.

$lines = explode("\n", $_POST['names']);
print_r($lines);
Sign up to request clarification or add additional context in comments.

2 Comments

@Ron, if it helps you, click on the V on the left
I'll be sure to do that in 8 minutes when I can!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.