As you saw in the previous segment, the "TextHelper" class that I just built boasts limited functionality right now. To fix this issue, I'm going to enhance its capabilities by coding some additional methods, which will be tasked with applying a few useful text-formatting filters to an inputted string. This being explained, please pay close attention to the corresponding definitions of these additional methods, which look like this: // convert new lines to 'div' tags in string (id and class attributes can also be specified) public function newline_div($str, $id = '', $class = '') { if (is_string($str) AND empty($str) === FALSE) { if ($id != '') { $id = ' id="' . $id . '"'; } if ($class != '') { $class = ' class="' . $class . '"'; } return '<div' . $id . $class . '>' . str_replace("n", '<div></div>', $str) . '</div>'; } }
// uppercase all characters in string public function uppercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtoupper($str); } }
// lowercase all characters in string public function lowercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtolower($str); } } // uppercase first character in string public function uppercase_first($str) { if (is_string($str) AND empty($str) === FALSE) { return ucfirst(strtolower($str)); } } As you can see above, I defined four extra methods for the text helper class. They apply some trivial formatting filters to a supplied string, such as converting its new lines into '<div>' tags, and upper-casing and lowercasing its characters respectively. Regardless of the rather rudimentary logic implemented by these methods, they do demonstrate how easy it is to create a helper class. From this point onward, and by using this example class as a reference, you may want to start developing your own text helper right now. But before you do that, I suggest you take a look at the full source code of the helper class, which looks much more functional after adding the additional methods discussed previously. Here's the complete class: class TextHelper { // constructor (not implemented) public function __construct(){}
// convert new lines to '<br />' tags public function newline_br($str) { if (is_string($str) AND empty($str) === FALSE) { return nl2br($str); } }
// convert new lines to '<p>' tags in string public function newline_par($str, $id = '', $class = '') { if (is_string($str) AND empty($str) === FALSE) { if ($id != '') { $id = ' id="' . $id . '"'; } if ($class != '') { $class = ' class="' . $class . '"'; } return '<p' . $id . $class . '>' . str_replace("n", '</p><p>', $str) . '</p>'; } }
// convert new lines to 'div' tags in string (id and class attributes can also be specified) public function newline_div($str, $id = '', $class = '') { if (is_string($str) AND empty($str) === FALSE) { if ($id != '') { $id = ' id="' . $id . '"'; } if ($class != '') { $class = ' class="' . $class . '"'; } return '<div' . $id . $class . '>' . str_replace("n", '<div></div>', $str) . '</div>'; } }
// uppercase all characters in string public function uppercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtoupper($str); } }
// lowercase all characters in string public function lowercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtolower($str); } } // uppercase first character in string public function uppercase_first($str) { if (is_string($str) AND empty($str) === FALSE) { return ucfirst(strtolower($str)); } } } Well, at this point I'm pretty sure that you understand the logic behind creating this basic text helper class. So if you want to see how it can be used for filtering a few strings, in the last section of this article I'm going to set up a concrete example for you, which will demonstrate the actual capabilities of the helper. To see how this wrapping example will be developed, click on the link shown below and read the next few lines. We're almost finished!
blog comments powered by Disqus |
|
|
|
|
|
|
|