Programming Basics

  Home arrow Programming Basics arrow Page 28 - 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 - Manually Stripping Tags


    (Page 28 of 37 )

    Earlier, we looked at the 'strip_tags()' function to remove HTML tags from user input. But, what would happen if one day you woke up and that function no longer existed? Let's examine how to duplicate some of its functionality with string functions.

    <?php
    $text 
    "The &lt;b&gt;word&lt;/b&gt; is bolded.&lt;br /&gt;
    Here is a &lt;a href=\"page.php\"&gt;link&lt;/a&gt;."
    ;

    while (
    substr_count ($text'&lt;') != 0) {
        $start strpos ($text'&lt;');
        $end strpos ($text'&gt;');
        $length = ($end $start) + 1;
        $text substr_replace ($text''$start$length);
    }
    echo 
    $text;
    ?>

    This would output:

    The word is bolded. Here is a link.

    Now, how exactly does this work? Well, we run a while loop whose condition is that there is at least once occurrence of the less than symbol in the text. As long as one less than symbol remains, the loop will continue. Within the loop, we use the 'strpos()' function to obtain the position of the first less than symbol and the first greater than symbol. These two symbols would make up the beginning and end of a HTML tag. We then find the length between the two positions.

    After we have gathered these values we use the 'substr_replace()' function to replace the substring, specified by the start and length values, with an empty string. If there are still less than symbols remaining, the loop continues. The shortcomings of this code are if there is a less than symbol with no matching greater than symbol the loop will execute forever and if a greater than symbol appears prior to a less than symbol we will have data loss. We will address the infinite looping problem, but leave the second problem for you to develop a solution to with the knowledge of regular expressions you will gain later in your programming career.

    To solve our infinite loop problem, we can simply set the length value to one when we have a situation where there is not a matching greater than symbol. Currently, we are getting a value for '$start' and '$end' is getting a value of 'false', which evaluates to zero. So, our length always ends up being a negative number. By checking to see if '$end' is equal to false and setting '$length' to one if it is, we can avoid the infinite loop.

    <?php
    $text 
    "The &lt;b&gt;word&lt;/b&gt; is bolded.&lt;br /&gt;
    Here is a &lt;a href=\"page.php\"&gt;link&lt;/a&gt;&lt;."
    ;

    while (
    substr_count ($text'&lt;') != 0) {
        $start strpos ($text'&lt;');
        $end strpos ($text'&gt;');
        if ($end === false) {
            $length 1;
        } else {
            $length = ($end $start) + 1;
        }
        $text substr_replace ($text''$start$length);
    }
    echo 
    $text;
    ?>

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