Programming Basics
  Home arrow Programming Basics arrow Page 18 - 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 - Adding and Removing Slashes


    (Page 18 of 37 )

    The functionality of the 'addslashes()' and 'stripslashes()' functions is straightforward and to the point. Any data passed to the 'addslashes()' function will be returned with the characters mentioned earlier escaped. Conversely, passing data that has characters escaped by slashes to the 'stripslashes()' function will remove said slashes. Let's take a look at a couple of simple examples to demonstrate these points.

    <?php
    $text 
    "I'm going home.";
    $slashed addslashes ($text);
    echo 
    $slashed;
    ?>

    The output from this code would be:

    I\'m going home.

    Now, let's do the reverse to see how the slashes are removed.

    <?php
    $slashed 
    "I\'m going home.";
    $text stripslashes ($slashed);
    echo 
    $text;
    ?>

    From which the output would be:

    I'm going home.

    When should you add slashes?

    Adding slashes to your data is needed in situations that you will be storing that data in a database. If you don't properly escape data with slashes before inserting it into a database, your queries run the risk of failing. The key is knowing when you should or should not add slashes.

    Knowing when to add and remove slashes from strings has been a subject of confusion for many PHP programmers. The culprit behind the confusion is the 'magic_quotes_gpc' directive. This is a setting that can be configured through the PHP configuration file. It can also be set for a particular directory on the web server or on a script by script basis by using the ini_set()' function. When this directive is enabled PHP will automatically add slashes to certain data, making it unnecessary for us to do so. The confusion comes in because programmers don't know when they should, and when they shouldn't, add slashes.

    To eliminate this confusion, let's first examine the 'magic_quotes_gpc' directive and determine when and how it operates. The data that will be escaped when this directive is enabled is any data from a GET, POST, or cookie operation. So, any data received from a HTML form, data that comes from variables specified in the URL's query string, and any data from a cookie will be escaped with slashes.

    When this directive is enabled, it will operate just as if we had sent the data through the 'addslashes()' function. If we run the same data through the 'addslashes()' function, the data will end up with too many backslashes and confusion will set in. Luckily, there is an easy way for us to determine if we need to add slashes or not. PHP supplies us with a function named 'get_magic_quotes_gpc()'. This function will return a '0' if the 'magic_quotes_gpc' directive is off and a '1' if it is on. By utilizing this function, we can dynamically decide whether we should add slashes or not.

    <?php
    if (!get_magic_quotes_gpc()) {
        $data addslashes ($_POST['somvariable']);
    } else {
        $data $_POST['somvariable'];
    }
    ?>

    The code snippet above shows how we can use the 'get_magic_quotes_gpc' function to determine if adding slashes is necessary. In this example, we are taking data from a form via the POST method. Remember, 'magic_quotes_gpc' only affects data that comes from the GET, POST, and cookie methods. All other data will not be affected and should be properly escaped with 'addslashes()'.

    There are two other functions that are very similar to addslashes() and stripslashes(). They are addcslashes() and stripcslashes(). The difference of these two functions is they will escape many more characters than the originally presented functions. They also require that you provide a list of characters that should be escaped. For most purposes, the normal addslashes() and stripslashes() functions will do all you need and there shouldn't be a need to use the second pair of functions we have just mentioned.

    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 1 Hosted by Hostway
    Stay green...Green IT