Home arrow PHP arrow Page 3 - Building Helpers in PHP 5

Extending the functionality of the text helper class - PHP

Tired of repetitive coding for tasks such as generating web site breadcrumbs? PHP 5 provides a number of interesting ways to reuse your code so you can let the boring stuff take care of itself. This eight-part series will show you how to create helpers to take care of repetitive tasks and free up your precious coding time for the real challenges.

TABLE OF CONTENTS:
  1. Building Helpers in PHP 5
  2. Building a helper in PHP 5
  3. Extending the functionality of the text helper class
  4. The text helper class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
July 20, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> 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: