Programming Basics

  Home arrow Programming Basics arrow Page 24 - 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 - Extracting Substrings


    (Page 24 of 37 )

    Many times we don't want to deal with an entire string. We may want to extract just a portion of it to work with or store into another string. The substr()' function provides us with this functionality. With it, we can extract any portion of a larger string that we wish. It accepts three parameters, the string to work with, a starting point, and a length. The length is optional, and if it is not specified 'substr()' will return the portion of the larger string from the start point to the end.

    Now, let's take a look at a couple of simple examples so that we may understand how 'substr()' works.

    <?php
    $mystring 
    'Hello World!';
    echo 
    substr($mystring2);
    echo 
    "&lt;br /&gt;\n";
    echo 
    substr($mystring23);
    echo 
    "&lt;br /&gt;\n";
    echo 
    substr($mystring, -4);
    echo 
    "&lt;br /&gt;\n";
    ?>

    Output:

    llo World!
    llo
    rld!

    The first call to 'substr()' specifies a starting point of 2. Everything starts counting at zero, so the number 2 specifies the third character. The first call will return the portion of $mystring from the third character to the end of the string. In the second call, we have specified the length. This will limit the string that is returned to only 3 characters.

    The last call to 'substr()' is a little different than the others. You will notice that we specified a negative number. This tells the function to start counting from the end of the string. In this case, the function will return the last four characters of the string. If we had specified a length, we could have limited the number of characters it returned.

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