+ Reply to Thread
Results 1 to 8 of 8

Thread: Count special characters with strpbrk function (PHP)

  1. #1
    Fearghal's Avatar
    Fearghal is offline x10 Lieutenant Fearghal is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    286

    Count special characters with strpbrk function (PHP)

    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.

  2. #2
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: Count special characters with strpbrk function (PHP)

    Like
    $phrase = 'The quick brown fox jumped over the lazy dog' ;
    $vowels = 'aeiou' ;

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

    ????

  3. #3
    Fearghal's Avatar
    Fearghal is offline x10 Lieutenant Fearghal is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    286

    Re: Count special characters with strpbrk function (PHP)

    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

  4. #4
    Fearghal's Avatar
    Fearghal is offline x10 Lieutenant Fearghal is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    286

    Re: Count special characters with strpbrk function (PHP)

    I found the substr_count function, do you think this would work?...

  5. #5
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: Count special characters with strpbrk function (PHP)

    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?

  6. #6
    Fearghal's Avatar
    Fearghal is offline x10 Lieutenant Fearghal is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    286

    Re: Count special characters with strpbrk function (PHP)

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

    How exactly does the preg_match_all function work?

  7. #7
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: Count special characters with strpbrk function (PHP)

    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 []

  8. #8
    Fearghal's Avatar
    Fearghal is offline x10 Lieutenant Fearghal is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    286

    Re: Count special characters with strpbrk function (PHP)

    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 by Fearghal; 06-11-2010 at 05:15 PM.

+ Reply to Thread

Similar Threads

  1. Help with these chinese characters..
    By vekou in forum Off Topic
    Replies: 2
    Last Post: 04-06-2010, 11:26 PM
  2. Avatars made *Characters not done by me*
    By JXVForums in forum Graphics & Webdesign
    Replies: 8
    Last Post: 01-16-2009, 03:41 AM
  3. Chinese characters??
    By vinzai in forum Free Hosting
    Replies: 5
    Last Post: 10-23-2008, 03:48 PM
  4. IE spanish characters
    By fuzzysystems in forum Programming Help
    Replies: 5
    Last Post: 09-16-2008, 10:43 PM
  5. Password characters allowed?
    By Veridis in forum Free Hosting
    Replies: 2
    Last Post: 03-28-2008, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers