Programming Basics

  Home arrow Programming Basics arrow Page 19 - 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 - Dealing with HTML Tags and Entities


    (Page 19 of 37 )

    Making sure you properly handle user input with regards to HTML tags and entities is crucial. If you do not handle these special characters properly, you will end up with your web pages looking far different than you had planned and there is the possibility of code arbitrarily being executed on your server. The major vulnerability here is with something called cross site scripting, or XSS. This can allow a person to cause some action to occur from your web site that you did not intend. A common exploit of XSS is to steal the cookies your site issues to users. It is, therefore, very important that we properly handle user input.

    In PHP, we have a couple of different options on how to deal with these situations. First, we can simply strip the tags out of the data. Or, rather than removing the tags, we can change the characters in the tags to their HTML entity equivalents so that we can display them.

    Removing the tags

    In some situations, any type of HTML or PHP tag is simply unacceptable. If you plan to display one user's input to other users on your web site, it is advisable that you remove HTML tags from the input. With the 'strip_tags()' function we can easily remove any and all tags from a string. This function also has an optional second parameter to specify tags that should be allowed. First, let's take a look at an example where will strip all tags from a string.

    <?php
    $userinput 
    "I &lt;b&gt;love&lt;/b&gt; chocolate!&lt;br /&gt;\n" .
                 "&lt;a href=\"/fotd.html\"&gt;Click here&lt;/a&gt;";

    $userinput strip_tags ($userinput);
    echo 
    $userinput;
    ?>

    This would output:

    I love chocolate! Click here

    The three different tags we used within the string have all been stripped out. When we don't specify the second parameter for 'strip_tags()', it throws caution to the wind and removes anything that resembles a tag.

    There are cases where certain tags might be acceptable. In the case of the example above, we might allow the '<b>' and the '<i> 'tag. To do that, we would simply pass the 'strip_tags()' function the second parameter as a string containing the acceptable tags.

    <?php
    $userinput 
    "I &lt;b&gt;love&lt;/b&gt; chocolate!&lt;br /&gt;\n" .
                 "&lt;a href=\"/fotd.html\"&gt;Click here&lt;/a&gt;";

    $userinput strip_tags ($userinput,"&lt;b&gt;&lt;i&gt;");
    echo 
    $userinput;
    ?>

    Changing the tags

    PHP provides a couple of different methods for changing characters to their HTML entity equivalents. This allows us to change the characters used in HTML and PHP tags into a form that we can display without the tags being interpreted. In some cases such as a forum where users share code, this is preferable to stripping the tags out.

    There are two different functions we can use to translate characters into their HTML entity equivalents. The first, 'htmlentities()', will translate all characters which have a HTML entity equivalent. For most applications, this is overkill. The only characters we normally need to worry about are the ones that the second function, 'htmlspecialchars()', translates.

    The 'htmlspecialchars' function will translate the following characters:

  • & (ampersand) into &amp;
  • " (double quote) into &quot;
  • < (less than) into &lt;
  • > (greater than) into &gt;

    Let's take a look at an example and see what it will translate the tags into.

    <?php
    $input 
    "&lt;?php echo 'Hello'; ?&gt;";
    $input htmlspecialchars($input);
    echo 
    $input;
    ?>

    In the browser, this will appear as:

    &lt;?php echo 'Hello'; ?&gt;

    If you view the source of that page, you will see this:

    &amp;lt;?php echo 'Hello'; ?&amp;gt;

    If we had not converted all the characters that made up the tags in their HTML entity equivalents, that string would not have displayed correctly.

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