Home arrow PHP arrow Page 2 - Developing a URL Handling Helper Class in PHP 5

Review: the previous TextHelper class - PHP

In this third installment of a series on building helpers in PHP 5, I will provide you with the basic pointers for building a URL-handling helper class. The code examples, though simple, should serve to demonstrate how to create a helper that generates dynamic URLs.

TABLE OF CONTENTS:
  1. Developing a URL Handling Helper Class in PHP 5
  2. Review: the previous TextHelper class
  3. Building a URL-handling helper class
  4. Putting the URL helper class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
August 03, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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