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.

6
  • 4
    Possibly because you need to explode() on the linefeed \n not the carriage return \r. Commented Feb 15, 2013 at 23:05
  • how do you know its \r not \n or \n? Commented Feb 15, 2013 at 23:06
  • 4
    works for me as expected: codepad.viper-7.com/wkYlhC Commented Feb 15, 2013 at 23:06
  • you could look after the PHP_EOL constant Commented Feb 15, 2013 at 23:09
  • 1
    Thanks it was a simple mistake i couldn't figure out Commented Feb 15, 2013 at 23:09

1 Answer 1

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

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

2 Comments

FYI: PHP_EOL is \n on all platforms. Not that it matters.
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.

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.