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


       · 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