PHP
  Home arrow PHP arrow Page 3 - Manipulating String Literals with Inte...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
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? 
PHP

Manipulating String Literals with Interpreter Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-04-09

    Table of Contents:
  • Manipulating String Literals with Interpreter Classes in PHP 5
  • Handling literals
  • Defining a string interpreter class
  • Parsing string commands by using the StringInterpreter class

  • 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


    Manipulating String Literals with Interpreter Classes in PHP 5 - Defining a string interpreter class


    (Page 3 of 4 )

    As I expressed in the section that you just read, the only piece required to complete the schema imposed by the interpreter pattern is an interpreter class. This class should be capable of parsing a bunch of predefined commands to process a given data string in some useful way.

    Below I listed the definition of a brand new interpreter class. As I explained previously, it will be capable of parsing a number of specific string commands.

    Please examine the signature of this string interpreter class, which looks like this:

    // define 'StringInterpreter' class
    class StringInterpreter{
       private $stringSaver;
       public function __construct(StringSaver $stringSaver){
         $this->stringSaver=$stringSaver;
       }
       // parse string commands
       public function interpret($command){
         if($command!='lowercase'&&$command!='uppercase'&&$command!
    ='reverse'&&$command!='length'){
           throw new Exception('A valid user command must be supplied
    to parse data string!');
         }
         // parse 'lowercase' command
         elseif($command=='lowercase'){
           return strtolower($this->stringSaver->loadString
    ());           
         }
         // parse 'uppercase' command
         elseif($command=='uppercase'){
           return strtoupper($this->stringSaver->loadString());
         }
         // parse 'reverse' command
         elseif($command=='reverse'){
           return strrev($this->stringSaver->loadString());
         }
         // parse 'length' command
         else{
           return strlen($this->stringSaver->loadString());
         }
       }
    }

    As you can see, the logic implemented by the prior “StringInterpreter” class is fairly easy to follow. Similar to the interpreter that I created in the preceding article of the series, this class also presents a method, called “interpret(),” which not surprisingly is tasked with parsing a predefined number of string commands.

    In this case, the class in question has been provided with the ability to execute four different instructions, named “lowercase,” “uppercase,” “reverse” and finally “length.” Obviously, since the names of these commands are extremely intuitive, I’m sure that you’ve already guessed that they’re used to uppercase, lowercase and reverse a given input string, in addition to calculating its length.

    Nevertheless, it’s worthwhile to note here that all these simple string-related operations are performed internally by the interpreter by using the “loadString()” method that belongs to the “StringSaver” class. Simple to understand, right?

    Okay, at this point I believe that you already understand how the previously-defined string interpreter class works. At this point I imagine you want to see for yourself how this class can be used in a concrete situation. Therefore, in the upcoming section I’m going to develop a simple example to demonstrate how the class in question can be capable of parsing the wealth of string commands that were discussed earlier.

    To see how this practical example will be created, click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this second installment of the series, you'll see how to use an...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





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