将字符串拆分为平面字符数组
我已经做了很多谷歌搜索之类的事情,但找不到我想要的东西......
我正在努力加强我的网站的身份验证。我决定获取用户的凭据,并对它们进行哈希/加盐处理。然后将这些值存储在数据库和用户cookie中。我修改了在 PHP 网站上找到的脚本,到目前为止效果很好。然而,我注意到在使用 array_rand() 时,它会按顺序从预定义的字符串中选择字符。我不喜欢这样,所以我决定对 array_rand() 数组使用随机播放。效果很好。
下一个!我认为将用户输入的密码转换为数组,然后将其与我的加盐数组合并是很聪明的!好吧,我在将用户密码转换为数组时遇到问题。我希望密码中的每个字符都是一个数组条目。 IE,如果您的密码是“cool”,则数组将为,Array 0 => c 1 => o 2 => o 3 => l 等等等
我尝试过用 word 来拆分字符串,然后用指定的中断字符将其分解,但这不起作用。我想我可以用 for 循环、strlen 之类的东西来做一些事情,但必须有一种更优雅的方式。
有什么建议吗?我有点困惑:(这是我到目前为止所拥有的,我还没有完成这个,因为我还没有比爆炸部分取得进一步的进展。
$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 />";
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 />";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要的php函数是str_split
,它会返回,用法如上
The php function you want is str_split
And it would return, is used as above
由于 PHP 的松散类型,如果您将字符串视为数组,PHP 将很乐意执行您所期望的操作。例如:
Thanks to PHP's loose typing if you treat the string as an array, php will hapilly do what you would expect. For example:
永远、永远不要实现(或者在这种情况下,也设计!)加密算法,除非您真的知道自己在做什么。如果您决定继续这样做,那么您的网站就会面临风险。
您没有理由必须这样做:肯定已经有库和/或函数可以完成所有此类事情。
Never, EVER implement (or in this case, design too!) cryptographical algorithms unless you really know what you are doing. If you decide to go ahead and do it anyways, you're putting your website at risk.
There's no reason you should have to do this: there is most certainly libraries and/or functions to do all of this sort of thing already.