As I said in the previous section, it's necessary to create an example that shows how to use the "TextHelper" class that you learned before to apply the filters to a given string. Therefore, please look at the following code sample, which illustrates how to accomplish this process in a simple manner. Here it is: // create an instance of TextHelper class $txthelper = new TextHelper(); // convert new lines to '<br>' tags echo $txthelper->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 $txthelper->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 $txthelper->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 $txthelper->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 $txthelper->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 $txthelper->uppercase_first('the shinning is a scary book of Stephen King!'); /* displays the following The shinning is a scary book of stephen king! */ Definitely, the "TextHelper" class isn't going to change the way you develop your PHP applications, but it'll hopefully show you how to use their methods for applying different formatting filters to a supplied literal. Of course, as I said before, the current functionality of the class can be largely improved, but this task will be left as homework for you, so you can entertain yourself for many hours. Final thoughts That's all for now. In this first article of the series, I offered a friendly introduction to building helper classes with PHP 5. As you saw earlier, this process is extremely straightforward; it's reduced to applying the basis of the object-oriented approach and nothing else. In the upcoming part, I'm going to explain how to take advantage of the functionality offered by the previous text helper class without creating an instance of it, simply by statically calling their methods. Don't miss the next tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|