Programming Basics
  Home arrow Programming Basics arrow Page 29 - PHP Strings Primer
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · comment
       · really goood work ,it covers all the major string functions.also explained with...
       · test'ng
     

    PROGRAMMING BASICS ARTICLES

    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP
    - An Overview of Arrays in PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway