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 <p>&Hello </p><p>this is a simple example of method chaining in PHP</p> */ 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|