4

I have a list (as a string, say $peoplelist) like this:

Name1
Name2
Name3

What I'm trying to do is separate that entire string into separate elements (where each line corresponds to a new element // i.e. Element 1 is "Name1", Element 2 is "Name2", etc.) and place them into an array (essentially, use lines breaks as delimiters and split that string into separate elements to be put into an array under unique indexes).

This is what I have so far:

# Step 1 - Fetch the list
$peoplelist = file_get_contents('http://domain.tld/file-path/')
//The Contents of $peoplelist is stated at the top of this question.
//(The list of names in blue)

# Step 2 - Split the List, and put into an array
$arrayX = preg_split("/[\r\n]+/", $playerlist);
var_dump($arrayX);

Now, using the code mentioned above, this is what I get (output):

array(1) { [0]=> string(71) "Name1
Name2
Name3
" }

According to my output, (from what I understand) the entire string (the list from Step 1) is being placed into an array under a single index, which means that the second step isn't effectively doing what I intend it to do.

My question is, how do I split the list into separate elements (where each separate line on the original list is a unique element) and place these elements in an array?

EDIT: Thanks for all the help, everyone! And thank you, localheinz, for the solution :)

NOTE: If anyone reads this in the future, please ensure that your source file for the list contains raw data - my mistake was using a file with .php extension that contained html tags - this html script interfered with the indexing process.

2
  • 1
    "/\r\n|\n|\r/" should be "/[\r\n]+/" Commented Dec 29, 2015 at 3:07
  • Thanks for the correction Lashane :) Commented Dec 29, 2015 at 3:46

4 Answers 4

9

Use file():

$arrayX = file(
    'http://domain.tld/file-path/',
    FILE_IGNORE_NEW_LINES
);

For reference, see:

Sign up to request clarification or add additional context in comments.

10 Comments

this is the best of the answers (so far)
Thanks for the answer localheinz - I'll try it out, and let you know how it goes
It doesn't appear to work for me localheinz - the output displays an empty array.
Weird, can you provide more details?
Yes! That worked. Output: array(2) { [0]=> string(5) "Name1" [1]=> string(12) "Name2" } Thanks so much for your help localheinz, and thank you everyone else for your answers, I appreciate it! I've learnt a lot today. :)
|
5

You can use explode() in PHP. See this code below splitting strings using delimeter \n:

explode("\n", $string);

3 Comments

I'm not entirely sure but for windows the line break delimiter is \r\n for some particular cases.
delimeter is not a problem
Hey Ceeee - thanks for the answer, but that doesn't appear to work for me. I get the same output
3

Try it.

<?php

$peoplelist = file_get_contents('text.txt');
$arrayX[0] = $peoplelist;
echo "<pre>";
var_dump($arrayX);

?>

=> OUTPUT

  array(1) {
  [0]=>
  string(19) "Name1
   Name2
   Name3"
 }

3 Comments

Hey Mukesh, thank you for replying. This script added all the names under a single index - is there a way to separate so that [0]=> Name1, [1]=> Name2, etc.?
I may be misunderstanding something... if I am, please let me know! I'm assuming that [0], [1] and any number within a set of brackets refers to a index number... am I correct? Also, what does string(int) mean? Thanks again :)
Just figured out that the integer refers to the number of characters within the entry. It looks like html from my .php source file is interfering with the indexing process. Do you know a way to output the list into a raw format with only line breaks? Thanks in advance!
2

using explode with a delimiter PHP_EOL for cross platform.

$str = "Name1
Name2
Name3";

var_dump(explode(PHP_EOL,$str));

will result to

array(3) {
  [0]=>
  string(5) "Name1"
  [1]=>
  string(5) "Name2"
  [2]=>
  string(5) "Name3"
}

2 Comments

Be aware PHP_EOL is good for the server side, not to handle user input. It might work for him, might not.
Hey roullie - thanks for the answer. However, when I tried it, it didn't appear to work. Output: array(1) { [0]=> string(52) "Name1 Name2 Name3 " }

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.