Jump to content

Foreach Array adding "_" to values.


firemike

Recommended Posts

Weird issue I'm having here. I hope someone has answers :)

 

I'm reading from a text file 'servernames.txt' which looks like:

server1

server2

server3

 

I'm reading the files into a var:, then exploding into an array:

$servernames=file_get_contents("http://servernames.txt");

$server_array=explode("\n",$servernames);

 

A print_r shows exactly what I want:

print_r($server_array);

 

Array

(

    [0] => server1

    [1] => server2

    [2] => server3

)

 

Then I start the foreach function, the weirdness starts here.

foreach ($server_array as $server) {

$search=file_get_contents("http://webaddress/page.php?server=$server");

}

 

now if I echo $server within the foreach, I get the correct result. However when I put it onto my file_get_contents the server outputs to "server1_" and not "server1". This happens to server1 and server2, but not the last entry in the textfile server3. Server 3 works.

 

Now I have tried to change the text file to a space delimiter, changing the explode:

server1 server2 server3

 

And it works fine, it's only adding the "server1_" when the textfile is exploding into a newline.

 

Am I missing a step? I would prefer to add each entry on a new line but I don't understand why I am getting these added underscores.

 

Thanks for reading,

Mike

 

Link to comment
https://forums.phpfreaks.com/topic/50737-foreach-array-adding-_-to-values/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.