Programming Basics
  Home arrow Programming Basics arrow Page 14 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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 / 11
    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 - Reversing strings


    (Page 14 of 37 )

    The 'strrev()' function accepts a string and returns that same string in reverse order. All capitalization and punctuation remain the same within the string. This function is regularly used in algorithms and can be applied in simple word games.

    Palindromes

    A palindrome is a word that is spelled the same forward as backward. Using the 'strrev()' function, we can easily check to see if a word fits that condition. This example assumes that information is being posted to it from a HTML form.

    <?php
    $word 
    strtolower ($_POST['userinput']);
    if (
    $word == strrev ($word)) {
        echo 'The word is a palindrome';
    } else {
        echo 'This is not a palindrome';
    }
    ?>

    Notice that we also used to 'strtolower()' function in this example. We are accepting user input, so it is always best to be sure that we place the data in a state that we can validate. If the user were to enter a word with the first letter capitalized, it would not pass the comparison without the use of the 'strtolower()' function, even if it actually were a palindrome.

    The LUHN Formula

    A very common use for the 'strrev()' function is within an algorithm to check the validity of credit card numbers. The algorithm is called the LUHN mod 10 formula. This algorithm does not ensure the credit card itself is valid, just the number. It will report as valid a number that matches the algorithm but as not yet been issued to anyone. This formula comes in handy as a first line check before sending information to a credit card merchant for processing.

    The most common method for implementing the algorithm is to start by reversing the credit card number. Below is an implementation of the LUHN formula in PHP, using the 'strrev()' function. Several other string functions are used in this example, which we will cover at a later point.

    <?php
    $ccnum 
    strrev($ccnum);
    $total 0;

    for (
    $x 0$x &ltstrlen ($ccnum); $x++) {
      $digit substr($ccnum,$x,1);
      if ($x == 0) {
        $digit *= 2;
        if (strlen ($digit) == 2)
            $digit substr ($digit01) + substr ($digit11);
      }
      $total += $digit;
    }

    if (
    $total 10 == 0) print 'Good Credit Card Number';
    ?>

    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

    - PHP: Hypertext Preprocessor: What is it?
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT