Ajax software
Free javascripts
↑
Main Page
You create these functions and put them to work step by step in the following exercise.
Building the Link Factory
1.
In your
seophp
folder, create a new folder named
include
.
2.
In the
include
folder, create a file named
config.inc.php
, and add this code to it:
<?php
// site domain; no trailing ‘/‘ !
define(‘SITE_DOMAIN’, ‘http://seophp.example.com’);
?>
3.
Also in the
include
folder, create a file named
url_factory.inc.php
, and add the
_prepare_
url_text()
and
make_product_url()
functions to it as shown in the code listing:
<?php
// include config file
require_once ‘config.inc.php’;
// prepares a string to be included in an URL
function _prepare_url_text($string)
{
// remove all characters that aren’t a-z, 0-9, dash, underscore or space
$NOT_acceptable_characters_regex = ‘#[^-a-zA-Z0-9_ ]#‘;
$string = preg_replace($NOT_acceptable_characters_regex, ‘’, $string);
// remove all leading and trailing spaces
$string = trim($string);
// change all dashes, underscores and spaces to dashes
$string = preg_replace(‘#[-_ ]+#‘, ‘-‘, $string);
// return the modified string
return $string;
}
If you’re a PHP OOP (object-oriented programming) fan, you might like to place these
functions into a class. In this case, the
_prepare_url_text()
function should be a
private method because it’s only intended for internal use, and the
make_product_
url()
would be a public method. OOP features are not used in this chapter to keep
the examples simple and brief, but the underscore character is used to prefix func-
tions that are meant for internal use only — such as in
_prepare_url_text()
— to
make an eventual migration to object-oriented code easier. Later the book uses PHP
OOP features when that is recommended by the specifics of the exercises.
67
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 67
Ajax software
Free javascripts
→ R7