Count special characters with strpbrk function (PHP)

Fearghal

Member
Messages
286
Reaction score
0
Points
16
Hey guys, I now have a new problem.

Does anyone know how to use the strpbrk() in PHP to only count the letters in the char_list?

It's counting all characters after the char_list characters have been detected, is there a way to limit it to only those characters that match or even is there another function for this?

Thanks.
 

dlukin

New Member
Messages
427
Reaction score
25
Points
0
Like
$phrase = 'The quick brown fox jumped over the lazy dog' ;
$vowels = 'aeiou' ;

$numberOfVowels = mysteryfunction( $phrase , $vowels ) ; // answer: 11

????
 

Fearghal

Member
Messages
286
Reaction score
0
Points
16
Yes :)

Im making a password strength tester for the registration page on my site and want to increment the users "password score" with each next level.

1 upper case = $score++
2 upper case = $score++; $score++
1 special character = $score++
2 special characters = $score++; $score++
etc.

Although im very new to PHP and this is my "learning" project XD
 

dlukin

New Member
Messages
427
Reaction score
25
Points
0
Code:
$number_matches = preg_match_all($pattern , $password_candidate , $dummy );  // note: $dummy required but not used here

$pattern = '/[a-z]/' ;  // to count lower case letters
$pattern = '/[A-Z]/' ;  // to count CAPITAL LETTERS
$pattern = '/[0-9]/' ;  // to count digits

What sort of special characters are you going to allow?
 

Fearghal

Member
Messages
286
Reaction score
0
Points
16
I was thinking just the normal !"£$%^&*()_+~@:?><{},./\|`

How exactly does the preg_match_all function work?
 

dlukin

New Member
Messages
427
Reaction score
25
Points
0
preg_match_all( pattern , testString, matches )

takes a Perl regular expression and matches it against a string. After each match, it starts searching again from the end of the match. The matches are stored in the array passed to the function. The function returns the number of matches.

$pattern = '/[!@#$%^&*()]/' ; // will match one occurrence of any symbol inside the []
 

Fearghal

Member
Messages
286
Reaction score
0
Points
16
Thanks for the help :p

I fixed it.

Just for anyone who needs this, maybe this will help...

Code:
$scm = preg_match('/[^A-Za-z0-9]/', $password);
      if ($scm==1) $s++;

$scm means special character match
$s++ means increment the score.

[^A-Za-z0-9] this basically means anything except A-Z, a-z or numbers which means the only thing left is special characters.

If anyone needs more help with this let me know :)

@dlukin Thanks so much for your help... I wouldnt have been able to do that without you :)
 
Last edited:
Top