0

I have a list of products codes that i wish to read into an array using php.

the list is to be fetched from a website and has over 700 items looks something like this:

4310ABC
4590DEF
8950GHK

What i want to do is put every code into a php array like so:

php_array ( [0] => 4310ABC 
            [1] => 4590DEF 
            [2] => 8950GHK)

This is what i have:

$php_array = file_get_contents('http://anysite.net/product_codes.php');
print_r (explode("\n",$php_array));

But my result is :

Array ( [0] => 4310ABC
               4590DEF
               8950GHK)

I have tried explode, preg_split('/[\n\r]+/', $php_array); but nothing seems to do the trick. Can anyone give me some pointers? thanks!

9
  • 3
    What exactly do you get back from your file_get_contents() call? Are you sure that it's using \n as a separator, and not <br>, for example? Commented Sep 16, 2014 at 18:32
  • 1
    print the contents of $php_array before you explode and examine it. Commented Sep 16, 2014 at 18:32
  • 1
    its no good looking in the browser at the output, the items could be in <p></p> for example. right click, view source Commented Sep 16, 2014 at 18:39
  • 1
    so you need to explode on '<br>' not "\n" Commented Sep 16, 2014 at 18:43
  • 1
    looks like @Dany beat me to it accept it instead Commented Sep 16, 2014 at 18:47

2 Answers 2

2

The lines are separated by a br, so use this instead:

$php_array = file_get_contents('http://anysite.net/product_codes.php');
print_r (explode("<br>",$php_array));

Don't forget to change the br to however it's spelled within the document you are fetching, for example it's often spelled like this:

<br />

Which is the most correct way to write it.

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

1 Comment

might want to trim the string or replace " " with "" to get rid of any whitespace as well
0

It would depend how your php file is echoing out the three values, so I am not sure how it is interpreting line breaks. Try echoing out the values with no line breaks, but separated by some other character like '*' or something, and then explode them along that and see if that works.

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.