Programming Basics
  Home arrow Programming Basics arrow Page 28 - 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  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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 / 7
    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 - 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


       · comment
       · really goood work ,it covers all the major string functions.also explained with...
       · test'ng
     

    PROGRAMMING BASICS ARTICLES

    - 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
    - An Overview of Arrays in PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway