Programming Basics

  Home arrow Programming Basics arrow Page 26 - 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 - Filtering Words


    (Page 26 of 37 )

    When we accept input from users that we plan to display on our web site, we can't always be sure that what the user inputs is acceptable content. It becomes necessary to filter the data, for instance to try and ensure that foul language is not displayed on our web site where others can see it.

    The 'str_replace()' function is a perfect match for this situation. It allows you to search in a string for any substring and replace it with any string you provide. All the parameters for the 'str_replace()' function can be a string, or an array. This allows added flexibility in that we can provide multiple substrings to search for at one time. Let's take a look at how we can incorporate data filtering.

    <?php
    $text 
    "I am a badword and will be replaced.&lt;br /&gt;
    More bwrds are in this bdlywrded sentence"
    ;

    $badwords = array ('badword''bwrds''bdlywrd');

    $text str_replace ($badwords'!@#$%'$text);

    echo 
    $text;
    ?>

    This code would output:

    I am a !@#$% and will be replaced.
    More !@#$% are in this !@#$%ed sentence

    All the words specified in the '$badwords' array have been replaced with !@#$%.

    If the third parameter, the subject of the operation, is an array, then each element of the array will have replacements made to it.

    There are drawbacks to this approach worth mentioning however. The substr_replace()' function does not worry about whether the substring is an entire word or not. Because of this if a word in the '$badwords' array is a part of another word, the filtering will take place in the middle of the other word. One way to combat this side effect is to add a space before each word in the '$badwords' array. This will solve one problem but add another. The code will no longer catch bad words that start the string or come immediately before punctuation.

    Regular expressions provide a more complete solution at the cost of speed. The string functions covered in this tutorial are much faster than regular expressions.

    More Programming Basics Articles
    More By Matt Wade

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap