Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.