0

I have a HTML form and there is a textarea let's say like this:

link1
link2 
link3

I would like to take the text from the text area row after row through PHP file so it will in the end be like this:

$var1 will be "link1"
$var2 will be "link2"
$var3 will be "link3"

important - this variables must be saved in php - whole form is in html file

EDIT: this can be done with array too, so i will have ["link1", "link2", "link3"]

EDIT2: if this is done with array i would like to acces it with indexes like in JS or other languages so "$array[0]" for example... (i know its not done like this in php)

5
  • 1
    So explode text by a newline. Commented Feb 2, 2018 at 21:00
  • Well explode makes an array, OP wants separate variables by some odd reason. But array is a far better solution though. Commented Feb 2, 2018 at 21:10
  • maybe i could try this with array, could you please show me how this can be done with array? i googled php explode text but i dont use php daily, this is just for easy json file update Commented Feb 2, 2018 at 21:15
  • ok i got it now and did it Commented Feb 2, 2018 at 22:35
  • Mark it as solved? Posting the solution to your problem would be beneficial to other users who may visit this question in the future too. Commented Feb 3, 2018 at 2:58

1 Answer 1

1

In this case you can use a foreach.

Let's say you have a textarea tag.

<textarea name="lyrics"></textarea>

You can access it's value:

$lyrics = $_POST['lyrics'];
$text = trim($lyrics);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim');

Use foreach

foreach ($textAr as $line)
{
    echo $line;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.