Making keyword rich, human readable URLs
Posted: Wed Sep 20, 2006 1:48 am
This short class will turn names/titles into safe, keyword rich URLs for use with Apache's mod rewrite. Could be useful for blogs or forums.
Usage Examples
- Before - showforum.php?forumid=21
- After - forums/21/world-news-and-current-events/index.php
- Before - showthread.php?threadid=22107
- After - forums/view-topic/22107/yet-another-school-shooting.php
Sample Apache mod rewrite Rule
I have adjusted this code and taken into considerations all posts, so this is the freshest code.
The Code
Test 1
Output 1
We'll change a few properities of the object in the test.
Test 2
Output 2
Real World Project Usage
Usage Examples
- Before - showforum.php?forumid=21
- After - forums/21/world-news-and-current-events/index.php
- Before - showthread.php?threadid=22107
- After - forums/view-topic/22107/yet-another-school-shooting.php
Sample Apache mod rewrite Rule
Code: Select all
RewriteEngine On
RewriteRule ^forums/([0-9]+)/.+/index.html$ /forum.php?forumid=$1The Code
Code: Select all
<?php
/*
* This short class will turn user entered titles into URLs
* that are keyword rich and human readable. For use with
* Apache's mod rewrite.
*
* Author - scottayy@gmail.com
*/
class safeurl
{
//decode html entities in string?
//param boolean $decode
var $decode = true;
//charset to use if $decode is set to true
//param string $decode_charset
var $decode_charset = 'ISO-8859-1';
//turns string into all lowercase letters
//param boolean $lowercase
var $lowercase = true;
//strip out html tags from string?
//param boolean $strip
var $strip = true;
//maximum length of resulting title
//param int $maxlength
var $maxlength = 50;
//if maxlength is reached, chop at nearest whole word? or hard chop?
//param boolean $whole_word
var $whole_word = true;
//what title to use if no alphanumeric characters can be found
//param string $blank
var $blank = 'no-title';
//the worker function
//param string $text
function make_safe_url($text)
{
//prepare the string according to our options
if($this->decode)
{
$text = html_entity_decode($text,ENT_QUOTES,$this->decode_charset);
}
if($this->lowercase)
{
$text = strtolower($text);
}
if($this->strip)
{
$text = strip_tags($text);
}
//filter
$text = preg_replace("/[^&a-z0-9_-\s]/i",'',$text);
$text = str_replace(array('&',' '),array(' and ','-'),trim($text));
$text = preg_replace("/-{2,}/",'-',$text);
//chop?
if(strlen($text) > $this->maxlength)
{
$text = substr($text,0,$this->maxlength);
if($this->whole_word)
{
$text = explode('-',$text);
$text = implode('-',array_diff($text,array(array_pop($text))));
}
}
//return =]
if($text == '')
{
return $blank;
}
return $text;
}
}
?>Code: Select all
$safeurl = new safeurl();
$tests = array(
'i\'m a test string!! do u like me. or not......., billy bob!!@#',
'<b>some HTML</b> in <i>here</i>!!~',
'i!@#*#@ l#*(*(#**$*o**(*^v^*(e d//////e\\\\\\\\v,,,,,,,,,,n%$#@!~e*(+=t',
'A lOng String wiTh a buNchess of words thats! should be -chopped- at the last whole word'
);
foreach($tests AS $test)
{
echo $safeurl->make_safe_url($test).'<br />';
}Code: Select all
im-a-test-string-do-u-like-me-or-not-billy-bob
some-html-in-here
i-love-devnet
a-long-string-with-a-bunchess-of-words-thatsTest 2
Code: Select all
$safeurl = new safeurl();
$safeurl->lowercase = false;
$safeurl->whole_word = false;
$tests = array(
'i\'m a test string!! do u like me. or not......., billy bob!!@#',
'<b>some HTML</b> in <i>here</i>!!~',
'i!@#*#@ l#*(*(#**$*o**(*^v^*(e d//////e\\\\\\\\v,,,,,,,,,,n%$#@!~e*(+=t',
'A lOng String wiTh a buNchess of words thats! should be -chopped- at the last whole word'
);
foreach($tests AS $test)
{
echo $safeurl->make_safe_url($test).'<br />';
}Code: Select all
im-a-test-string-do-u-like-me-or-not-billy-bob
some-HTML-in-here
i-love-devnet
A-lOng-String-wiTh-a-buNchess-of-words-thats-shoulCode: Select all
echo '<a href="blog/12/'.$safeurl->make_safe_url($blog_title).'">'.$blog_title.'</a>';