pokrak94
pokrak94

Reputation: 202

How to iterate over multiline text in text box in php?

What I am trying to do is I want to write a little helper application for myself, which will convert list of coordinates into list of coordinates in bb code, like so:

This is what user will input:

123|456

And the output will be:

1. [coord]123|456[/coord]

So basically I need something that will allow me to store the multiline text as a php variable, and then to iterate over that variable changing each line like in the example above.

Please help :)

Upvotes: 3

Views: 1047

Answers (1)

Luděk Veselý
Luděk Veselý

Reputation: 459

You can create simple form:

<form method="post">
    <textarea name="multilineData"></textarea>
    <input type="submit" value="Submit">
</form>

Than you can process submitted form:

<?php
$output = '';
$num = 1;
foreach (explode(PHP_EOL, $_POST['multilineData']) as $row) {
    $output .= "$num. [coord]$row[/coord]<br>";
    $num++;
}
echo $output;

Upvotes: 3

Related Questions