Php foreach character in string

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.

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 example

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

Thanks, Matthy

asked Aug 18, 2009 at 13:43

1

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

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

answered Feb 14, 2013 at 19:34

KiyanKiyan

2,05515 silver badges15 bronze badges

4

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.

answered Aug 18, 2009 at 13:49

Tom HaighTom Haigh

56.6k20 gold badges111 silver badges140 bronze badges

3

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.

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

answered May 27, 2015 at 18:17

DanijelDanijel

12.1k4 gold badges36 silver badges53 bronze badges

0

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

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

nneonneo

165k35 gold badges294 silver badges368 bronze badges

answered Aug 18, 2009 at 13:48

GregGreg

310k53 gold badges365 silver badges328 bronze badges

1

Try this, It works beter for UTF8 characters (Kurdish, Persian and Arabic):

";}
?>

answered Feb 19 at 4:53

Php foreach character in string

chrochro

113 bronze badges

This is a simple guide on how to loop through the characters of a string in PHP.

First of all, you need to know about the function str_split.

Basically, this function converts a given string into an array. In other words, the string is “split up” and each character is then added as an element to an array.

Note that this will also include special characters and whitespaces.

A short example.

If you run the code above, you’ll see that the $myArray variable is an array that contains each character from our string.

Then, you just loop through it like so.

";
}

The code above will loop through the characters and print them out on separate lines.

As I said, it’s actually pretty simple!

How can I access all characters in a string in PHP?

PHP – Loop through Characters in String.
Take a string..
Split the string into an array of characters using str_split() function..
Use a looping statement like for loop or while loop, to iterate over each of the element (character) in the array..

How do I print a character from a string in PHP?

PHP has str_split () function, which is used to print every character of a string.

How do I count the number of repeated characters in a string in PHP?

To count specific characters in string or count repeated in a string PHP, you can use PHP function substr_count().

What is foreach in PHP?

The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.