JavaScript Editor Ajax software     Free javascripts 



Main Page

Finally, you use
preg_replace()
once again to transform any groups of spaces, dashes, and underscores
to dashes. For example, a string such as
SEO___Toolbox
(note there are three underscores) would be
replaced to
SEO-Toolbox
. Then you return the transformed URL string:
// change all dashes, underscores and spaces to dashes
$string = preg_replace(‘#[-_ ]+#‘, ‘-‘, $string);
// return the modified string
return $string;
Here are some examples of this transformation:
// displays Carpenters-Tools
echo _prepare_url_text(“Carpenter’s Tools”);
// displays Black-And-White
echo _prepare_url_text(“Black and White”);
_prepare_url_text()
is used by
make_category_product_url()
to create product URLs. The
make_category_product_url()
function receives as parameters a category name, a product name,
a category ID, and a product ID, and it uses this data to create the keyword-rich URL. The code in this
function is pretty straightforward. It starts by using
_prepare_url_text()
to prepare the product and
category names for inclusion in a URL:
// builds a link that contains a category and a product
function make_category_product_url($category_name, $category_id,
$product_name, $product_id)
{
// prepare the product name and category name for inclusion in URL
$clean_category_name = _prepare_url_text($category_name);
$clean_product_name = _prepare_url_text($product_name);
Then it simply joins strings to create a string of the form
./Products/
Category
-
Name
-C
m
/
Product
-
Name
/P
n
.html
. Finally, it returns this string:
// build the keyword-rich URL
$url = ‘./Products/‘ .
$clean_category_name . ‘-C’ . $category_id . ‘/‘ .
$clean_product_name . ‘-P’ . $product_id . ‘.html’;
// return the URL
return $url;
}
Here are some examples of this function’s use, and the results:
// display /Products/Carpenters-Tools-C12/Belt-Sander-P45.html
echo make_product_url(“Carpenter’s Tools”, 12, ‘Belt Sander’, 45);
// display /Products/Hammers-C3/Big-and-Mighty-Hammer-P4.html
echo make_product_url(‘Hammers’, 3, ‘Big and Mighty Hammer’, 4);
71
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 71


JavaScript Editor Ajax software     Free javascripts 
R7