TURTLE
TURTLE

Reputation: 3847

php foreach not working properly with explode

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.

Upvotes: 1

Views: 583

Answers (1)

Green Black
Green Black

Reputation: 5084

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

Upvotes: 3

Related Questions