Fireworksable
Fireworksable

Reputation: 343

Use line-break as separator for an array input?

I've never actually used arrays before, as I've never had to so far (a simple variable has been enough for me), however now I've created a form with a text-area that is meant to POST multiple urls through to my PHP script.

What I want to do is use a line-break in the visitors input to act as a separator for an array input.

For example, the visitor inputs 90 lines of text (all url's), the array breaks each one into a list of 90, and creates an array value for each one.

Any info, advice or comments would be greatly appreciated :)!

Upvotes: 0

Views: 3455

Answers (3)

PeeHaa
PeeHaa

Reputation: 72672

Not 100% percent sure what line breaks are used, e.g.:

Windows uses \r\n Linux uses \n (old) Macs used \r

However if you know this you can simply do:

$urls = explode("\n", $_POST['urls']);

EDIT

Actually after testing using regex IS faster than first doing a str_replace() and explode.

Upvotes: 1

RiaD
RiaD

Reputation: 47619

Use explode

$array=explode("\n",$_POST['textarea']);

Upvotes: -1

Marek Sebera
Marek Sebera

Reputation: 40631

Look at http://www.php.net/manual/en/function.preg-split.php and as delimiter use new line sign

or see PHP REGEX - text to array by preg_split at line break

be careful about using just \r or \n because every operating system has "new line" defined another way
see answer by Tgr on SO question PHP REGEX - text to array by preg_split at line break

Upvotes: 1

Related Questions