PHP:将字符串拆分为数组 foreach char

2022-01-22 00:00:00 validation arrays split passwords php

我正在制定一种方法,因此您的密码至少需要一个大写字母和一个符号或数字.我正在考虑将字符串拆分为丢失字符,然后使用 preggmatch 来计算它是否包含一个大写字母和符号/数字.

I am making a method so your password needs at least one captial and one symbol or number. I was thinking of splitting the string in to lose chars and then use preggmatch to count if it contains one capital and symbol/number.

但是我在动作脚本中做了类似的事情,但无法弄清楚这是如何在 php 中调用的.我找不到将单词的每个字符放入数组中的方法.

however i did something like this in action script but can't figure out how this is called in php. i cant find a way to put every char of a word in a array.

AS3 示例

    for(var i:uint = 0; i < thisWordCode.length -1 ; i++)
{
    thisWordCodeVerdeeld[i] = thisWordCode.charAt(i);
    //trace (thisWordCodeVerdeeld[i]);
}

谢谢,马蒂

推荐答案

您可以像访问数组索引一样访问字符串中的字符,例如

You can access characters in strings in the same way as you would access an array index, e.g.

$length = strlen($string);
$thisWordCodeVerdeeld = array();
for ($i=0; $i<$length; $i++) {
    $thisWordCodeVerdeeld[$i] = $string[$i];
}

你也可以这样做:

$thisWordCodeVerdeeld = str_split($string);

但是您可能会发现将字符串作为一个完整的字符串进行验证更容易,例如使用正则表达式.

However you might find it is easier to validate the string as a whole string, e.g. using regular expressions.

相关文章