Home arrow PHP arrow Page 3 - Completing a Sample String Processor with Method Chaining

Replacing characters of a string with HTML entities - PHP

In this third part of a 12-part series on method chaining, I complete the definition of the sample string processor class. This process will help reaffirm the concepts that you learned before regarding the definition and implementation of chainable methods in PHP 5.

TABLE OF CONTENTS:
  1. Completing a Sample String Processor with Method Chaining
  2. Review: the string processor class
  3. Replacing characters of a string with HTML entities
  4. Replacing and reversing characters of a literal
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
October 28, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

One of the last chainable methods that I plan to add to the string processor class is one responsible for replacing certain characters of an incoming string with their correct HTML entities. As you may have guessed, this method will act as a proxy for the "htmlentities()" PHP native function. Its definition is shown by the code sample below:

class StringProcessor

{

private $str = '';

private static $instance = NULL;

 

// factory method

// returns a singleton of string processor

// chainable

public static function factory($str)

{

if (self::$instance === NULL)

{

self::$instance = new StringProcessor($str);

}

return self::$instance;

}

 

// constructor

public function __construct($str = 'This is a default string')

{

$this->str= $str;

}

 

// trim white space from input string

// chainable

public function trim_space()

{

$this->str = trim($this->str);

return $this;

}

 

// uppercase input string

// chainable

public function str_upper()

{

$this->str = strtoupper($this->str);

return $this;

}

 

// lowercase input string

// chainable

public function str_lower()

{

$this->str = strtolower($this->str);

return $this;

}

 

// remove eventual HTML and PHP tags from input string

// chainable

public function remove_tags()

{

$this->str = strip_tags($this->str);

return $this;

}

 

// convert new lines to <br /> tags

// chainable

public function newline_br()

{

$this->str = nl2br($this->str);

return $this;

}

 

// convert new lines to paragraphs

// chainable

public function newline_par()

{

$this->str = '<p>' . str_replace("n", '</p><p>', $this->str) . '</p>';

return $this;

}

 

// parse special chars of input string

// chainable

public function parse_chars()

{

$this->str = htmlentities($this->str);

return $this;

}

 

// get input string

public function get_string()

{

return $this->str;

}

}

As depicted above, the "StringProcessor" class now implements a brand new chainable method called "parse_chars()." As I explained previously, this is a simple wrapper for the "htmlentities()" PHP function. Since this method can be easily chained to the existing ones, I've coded a simple example that shows how to use it in a concrete case. Here it is:

$strproc = new StringProcessor(' <strong>&Hello '. "n" . 'this is a simple example of method chaining in PHP</strong> ');

// process string by chaining multiple methods

echo $strproc->trim_space()->

remove_tags()->

newline_par()->

parse_chars()->

get_string();

/* displays the following

&lt;p&gt;&amp;Hello &lt;/p&gt;&lt;p&gt;this is a simple example of method chaining in PHP&lt;/p&gt;

*/

From the previous example, it's clear to see how the aforementioned "parse_chars()" method can be quickly chained to the others and replace some characters of the inputted string with their corresponding HTML entities. Simple to grasp, isn't it?

Now that you understand how the "parse_chars()" method does its business, it's time to finish building this sample string processor. With that idea in mind, in the last part of this tutorial I'm going to add to the class a couple of additional chainable methods for reversing the contents of a incoming string and for replacing certain characters with an arbitrary one.

To learn how these brand new methods will be defined, click on the link below and read the next few lines.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: