1

Ok some php code below.

$user_pass = "
vortex90:OPFY4MB8
jimmy3:3M7ISWof
dave-ish-mental:YEnMMXua
cindybaby:rRHxrErp
claire-x:H4VrT8Xx
icemonster:ODId9N17
";

$token = 'token';    
$ex = explode("\r", $user_pass);

foreach ($ex as $info) {
   print "username=" . str_replace(":", "&password=", $info) . "&token=" . $token . "\n";
}

What i want the foreach() to do is show for each explode

username=username&password=password&token=token

But below is what gets returned.

vortex90&password=OPFY4MB8
jimmy3&password=3M7ISWof
dave-ish-mental&password=YEnMMXua
cindybaby&password=rRHxrErp
claire-x&password=H4VrT8Xx
icemonster&password=ODId9N17

Why is it not returning as expected? all answers welcome.

TURTLE
  • 3,728
  • 4
  • 49
  • 50

1 Answers1

3

This works for me, it is better practice to use PHP_EOL:

$token = "bla";
$user_pass = "
vortex90:OPFY4MB8
jimmy3:3M7ISWof
dave-ish-mental:YEnMMXua
cindybaby:rRHxrErp
claire-x:H4VrT8Xx
icemonster:ODId9N17
";

$explode = explode(PHP_EOL, $user_pass);

foreach($explode as $i) {
$replace_shit  = str_replace(array("\r","\n",":"), array("","","&password="), $i);
$user_info = "username=".$replace_shit."&token=".$token."<br>\n";

echo $user_info;
}

DEMO: http://sandbox.onlinephpfunctions.com/code/02f6663f7fa69c158a90fde2ab421cf52a78f7ce

Green Black
  • 5,037
  • 1
  • 17
  • 29
  • FYI: `PHP_EOL` is `\n` on all platforms. Not that it matters. – Reactgular Feb 15 '13 at 23:32
  • Cool, I was looking at 5.3.2 in Core.php it's `define ('PHP_EOL', "\n");` for all platforms. Unless I'm reading it wrong. Still, if PHP read a Linux file saved on a Windows server, and then explode(PHP_EOL) it's contents would it then fail? Since the source file wouldn't contain `\r\n` line breaks? Windows's does not reformat text files and add missing `\r` breaks. – Reactgular Feb 15 '13 at 23:41