Programming Basics

  Home arrow Programming Basics arrow Page 29 - PHP Strings Primer
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2003-07-11

    Table of Contents:
  • PHP Strings Primer
  • The Basics
  • Single Quotes
  • Double Quotes
  • Heredoc
  • Concatenation
  • Displaying Strings
  • echo
  • print
  • printf
  • Strings Formatting
  • Preparing user input for comparisons
  • Capitalization
  • Reversing strings
  • Padding strings
  • Multiple Lines
  • Data Preparation
  • Adding and Removing Slashes
  • Dealing with HTML Tags and Entities
  • Counting
  • Checking password strength
  • Generating Statistics
  • Substrings (and searching)
  • Extracting Substrings
  • Counting Paragraphs
  • Filtering Words
  • Working with email addresses
  • Manually Stripping Tags
  • Password Strength Revisited
  • Handling URLs and Base64-encoding
  • Parsing URLs
  • Encoding for URLs
  • Encoding for Email
  • Hashing
  • Verifying Integrity
  • User Authentication
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    PHP Strings Primer - Password Strength Revisited


    (Page 29 of 37 )

    As we saw earlier in the tutorial, there are several methods we can use to determine if a password is a good password. Now, we will revisit the topic and demonstrate how we can determine if a password contains numbers in the middle of it, as is required in some security standards. For this task, we will make use of the 'strspn()' function.

    The 'strspn()' function accepts two strings as parameters. It will return the number of sequential characters at the beginning of the first string that match the characters in the second string given to the function. In order to demonstrate this, let's look at a simple example.

    <?php
    $mystring 
    '!!@# Attention!';
    $num strspn ($mystring'!@#$%^&amp;*()');
    ?>

    After this code executes, '$num' would contain the number '4'. The 'strspn()' function starts at the beginning of the string passed as parameter one and checks each character against the pattern found in parameter two. For each character found, without finding a character that does not match, the counter increments. Once a character is found that does not match, the number of matches is returned.

    As we mentioned, we will use this function to help test password strength. By counting the number of alphabetic characters before reaching a numeric character, we can determine if a password contains numbers surrounded by letters. To check the back side of the string, we will employ the use of the strrev()' function to reverse the password.

    <?php
    $password 
    'awsjua234sd';

    $mask "abcdefghijklmnopqrstuvwyxz" .
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    $front strspn ($password$mask);
    $back strspn(strrev($password), $mask);
    $strlength strlen($password);

    if ((
    $front &lt$strlength) &amp;&amp; ($back &gt0)) {
        echo 'Good Password!';
    } else {
        echo 'Bad Password!';
    }
    ?>

    There are other ways to accomplish this goal, for instance with regular expressions, but for our purposes this method will work just fine. As you can see, we first obtain the number of letters in the front, the number of letters in the back, and the length of the string. If the number of letters in front is less than the length of the string then we know that the entire string is not only letters. Then, we check to make sure at least one letter is at the back of the string. If both of these expressions evaluate true, then we have a good password.

    Another common password check is to make sure that a password does not start with a number. We could use the 'strspn()' function to perform this check in our above example by seeing if the '$front' variable was equal to zero. But, there is another function we would like to demonstrate that provides similar functionality.

    The 'strcspn()' function acts just as the 'strpn()' function, with the exception that it returns the number of sequential characters not in the second parameter. Let's see how we could make sure a password does not start with a number

    <?php
    $password 
    '12asfdgw';

    $mask "1234567890";
    $front strcspn ($password$mask);

    if (
    $front &gt0) {
        echo 'Good Password!';
    } else {
        echo 'Bad Password!';
    }
    ?>

    More Programming Basics Articles
    More By Matt Wade

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap