Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Break up word into array

Tags:

php

i've done plenty of googling and whatnot and can't find quite what i'm looking for...

I am working on tightening up the authentication for my website. I decided to take the user's credentials, and hash/salt the heck out of them. Then store these values in the DB and in user cookies. I modified a script I found on the PHP website and it's working great so far. I noticed however when using array_rand, that it would select the chars from the predefined string, sequentially. I didn't like that, so I decided to use a shuffle on the array_rand'd array. Worked great.

Next! I thought it would be clever to turn my user inputted password into an array, then merge that with my salted array! Well, I am having trouble turning my user's password into an array. I want each character in their password to be an array entry. IE, if your password was "cool", the array would be, Array 0 => c 1 => o 2 => o 3 => l, etc etc. I have tried word to split up the string then explode it with the specified break character, that didn't work. I figure I could do something with a for loop, strlen and whatnot, but there HAS to be a more elegant way.

Any suggestions? I'm kind of stumped :( Here is what I have so far, i'm not done with this as I haven't progressed further than the explodey part.

$strings = wordwrap($string, 1, "|");
echo $strings . "<br />";
$stringe = explode("|", $strings, 1);
print_r($stringe);
echo "<br />";
echo "This is the exploded password string, for mixing with salt.<hr />";

Thank you so much :)

like image 417
Shawn Avatar asked Jul 11 '11 22:07

Shawn


1 Answers

The php function you want is str_split

str_split('cool', 1);

And it would return, is used as above

[0] => c
[1] => o
[2] => o
[3] => l
like image 167
Colum Avatar answered Nov 15 '22 04:11

Colum