Programming Basics

  Home arrow Programming Basics arrow Page 14 - 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 - 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

    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 9 - Follow our Sitemap