PHP
  Home arrow PHP arrow Page 4 - Manipulating String Literals with Inte...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 - Parsing string commands by using the StringInterpreter class


    (Page 4 of 4 )

    If you’re anything like me, then you want to see how the previous string interpreter class can be used in the context of a hands-on example. Below I coded an easy-to-follow script, which demonstrates how different string commands are parsed by the corresponding interpreter, resulting in the manipulation of a specific input string.

    Also, it’s worth clarifying that the following example assumes that a sample string has been saved previously to a default target file. Now, examine the corresponding code listing, which looks like this:

    try{
       // create 'StringSaver' object
       $stringSaver=new StringSaver();
       // save sample string to destination file
       $stringSaver->saveString();
       // create 'StringInterpreter' object
       $stringInt=new StringInterpreter($stringSaver);
       // display lowercased input string
       echo $stringInt->interpret('lowercase');

       /* displays the following:
       this is the default string
       */

       // display uppercased input string
       echo $stringInt->interpret('uppercase');

       /*
       displays the following:
       THIS IS THE DEFAULT STRING
       */

       // display reversed input string
       echo $stringInt->interpret('reverse');

       /* displays the following:
       gnirts tluafed eht si sihT
       */

       // display length of input string
       echo 'Number of characters of input string is the following :
    '.$stringInt->interpret('length');

       /* displays the following:
       Number of characters of input string is the following : 26
       */         
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As shown above, the example first creates an instance of the “StringSaver” class, and uses its “saveString()” method to save a sample string to the default target file. Next, an interpreter object is spawned, and all the string commands accepted by this object are passed to the respective “interpreter()” method.

    Of course, the purpose in doing this is simply to uppercase, lowercase and reverse the inputted string, something that is clearly demonstrated by the subsequent calls to the “interpreter()” method.

    Finally, the example finishes displaying the number of characters corresponding to the aforementioned input string. Again, in this case the interpreter utilizes the predefined “length” command to perform this basic operation.

    So far, so good. At this stage, and after studying the source code that corresponds to the previous practical example, you should have a better idea of how the interpreter pattern does its business. As you saw, the logic behind this pattern is extremely simple, and doesn’t differ too much from creating a conventional parsing class.

    Final thoughts

    It’s hard to believe, but we’ve come to the end of this second article of the series. In this tutorial, you learned how to implement the interpreter pattern in PHP 5 by using a bunch of simple classes, aimed at processing different input strings in a basic way.

    Nonetheless, this is only the prologue for the next (and last) part of the series, since in it I’m going to demonstrate how this handy pattern can be used to develop a decent blog application. The experience sounds really promising and  educational. You won’t want to miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT