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

时间:2022-05-31 21:39:59

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.

我正在创建一个方法,以便您的密码至少需要一个属性和一个符号或数字。我正在考虑分割字符串以丢失字符,然后使用preggmatch来计算它是否包含一个大写和符号/数字。

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.

但是,我在action脚本中做了一些类似的事情,但是我不知道如何在php中调用它。我找不到一种方法把一个单词的每一个字符都放在一个数组中。

AS3 example

AS3的例子

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

Thanks, Matthy

谢谢,Matthy

5 个解决方案

#1


57  

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];
}

You could also do:

你也可以做的事:

$thisWordCodeVerdeeld = str_split($string);

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

但是,您可能会发现更容易将字符串验证为整个字符串,例如使用正则表达式。

#2


44  

you can convert a string to array with str_split and use foreach

可以使用str_split将字符串转换为数组并使用foreach

$chars = str_split($str);
foreach($chars as $char){
    // your code
}

#3


15  

You can access a string using [], as you do for arrays:

您可以使用[]访问字符串,就像对数组一样:

$stringLength = strlen($str);
for ($i = 0; $i < $stringLength; $i++)
    $char = $str[$i];

#4


6  

Since str_split() function is not multibyte safe, an easy solution to split UTF-8 encoded string is to use preg_split() with u (PCRE_UTF8) modifier.

由于str_split()函数不是多字节安全的,分割UTF-8编码字符串的一个简单解决方案是使用preg_split()与u (PCRE_UTF8)修饰符。

preg_split( '//u', $str, null, PREG_SPLIT_NO_EMPTY )

#5


1  

A less readable, but better performing solution, when iterating over many strings, might be using isset to check the end of the string. This might be better performing because isset is a language construct and strlen is a function:

当迭代多个字符串时,一个可读性较差但性能更好的解决方案可能是使用isset检查字符串的末尾。这可能表现得更好,因为isset是一种语言结构,而strlen是一个函数:

for ($i = 0; isset($value[$i]); $i++) {
    "do something here"
}

This question should provide more background.

这个问题应该提供更多的背景知识。

#1


57  

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];
}

You could also do:

你也可以做的事:

$thisWordCodeVerdeeld = str_split($string);

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

但是,您可能会发现更容易将字符串验证为整个字符串,例如使用正则表达式。

#2


44  

you can convert a string to array with str_split and use foreach

可以使用str_split将字符串转换为数组并使用foreach

$chars = str_split($str);
foreach($chars as $char){
    // your code
}

#3


15  

You can access a string using [], as you do for arrays:

您可以使用[]访问字符串,就像对数组一样:

$stringLength = strlen($str);
for ($i = 0; $i < $stringLength; $i++)
    $char = $str[$i];

#4


6  

Since str_split() function is not multibyte safe, an easy solution to split UTF-8 encoded string is to use preg_split() with u (PCRE_UTF8) modifier.

由于str_split()函数不是多字节安全的,分割UTF-8编码字符串的一个简单解决方案是使用preg_split()与u (PCRE_UTF8)修饰符。

preg_split( '//u', $str, null, PREG_SPLIT_NO_EMPTY )

#5


1  

A less readable, but better performing solution, when iterating over many strings, might be using isset to check the end of the string. This might be better performing because isset is a language construct and strlen is a function:

当迭代多个字符串时,一个可读性较差但性能更好的解决方案可能是使用isset检查字符串的末尾。这可能表现得更好,因为isset是一种语言结构,而strlen是一个函数:

for ($i = 0; isset($value[$i]); $i++) {
    "do something here"
}

This question should provide more background.

这个问题应该提供更多的背景知识。