Before I proceed to develop the URL-handling helper class that I just mentioned in the introduction, it'd be helpful to recall the signature and text helper created in the preceding article, so you can quickly grasp how it was defined and how it can be used for filtering strings. Having said that, here's the full source code of the text helper class, so examine it closely: class TextHelper { // constructor not implemented public function __construct(){}
// convert new lines to '<br />' tags public static function newline_br($str) { if (is_string($str) AND empty($str) === FALSE) { return nl2br($str); } }
// convert new lines to '<p>' tags in string public static 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 static 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 static function uppercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtoupper($str); } }
// lowercase all characters in string public static function lowercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtolower($str); } } // uppercase first character in string public static function uppercase_first($str) { if (is_string($str) AND empty($str) === FALSE) { return ucfirst(strtolower($str)); } } } If you're familiar with building PHP programs using an object-oriented approach, then you shouldn't have major problems understanding how the above "TextHelper" class does its thing. As you can see, it has some simple methods for applying different formatting processes to an inputted string. However, the most important thing to note here is that since those methods have been declared static, they also can be called out of the object scope. The following examples show how to invoke the methods in question without creating any instances of the text helper class. Here they are: // convert new lines to '<br />' tags echo TextHelper::newline_br('The shinning ' . "n" . 'is a scary book of Stephen King!'); /*displays the following The shinning <br /> is a scary book of Stephen King! */
// convert new lines to '<p>' tags echo TextHelper::newline_par('The shinning ' . "n" . 'is a scary book of Stephen King!', 'pid', 'pclass'); /* displays the following <p id="pid" class="pclass">The shinning </p><p>is a scary book of Stephen King!</p> */
// convert new lines to '<div>' tags echo TextHelper::newline_div('the shinning ' . "n" . 'is a scary book of Stephen King!', 'divid', 'divclass'); /* displays the following <div id="divid" class="divclass">the shinning <div></div>is a scary book of Stephen King!</div> */
// uppercase sample string echo TextHelper::uppercase_all('The shinning is a scary book of Stephen King!'); /* displays the following THE SHINNING IS A SCARY BOOK OF STEPHEN KING! */
// lowercase sample string echo TextHelper::lowercase_all('The shinning is a scary book of Stephen King!'); /* displays the following the shinning is a scary book of stephen king! */
// uppercase first character in sample string echo TextHelper::uppercase_first('the shinning is a scary book of Stephen King!'); /* displays the following The shinning is a scary book of stephen king! */ Aside from bringing up some great memories from my distant childhood (yeah, Stephen King was my favorite writer by that time), the examples shown previously show how to use the text helper in a more efficient way. Since these examples speak for themselves, I'm not going to waste your time explaining how they work. Instead, I'm going to continue discovering more useful things about building helper classes. Therefore, in accordance with the concepts deployed in the introduction, in the following section I'm going create another helper class, which will be tasked with generating dynamic URLs. To learn more about how this class will be built, click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|