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=$1
The 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 ','-',''),$text);
$text = trim(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;
}
}
?>
Test 1
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 />';
}Output 1
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-thatsWe'll change a few properities of the object in the test.
Test 2
Code: Select all
</span></li><li style=\"\" class=\"li2\"><span style=\"color: #ff0000;\">$safeurl = new safeurl(); </span></li><li style=\"\" class=\"li1\"><span style=\"color: #ff0000;\">$safeurl->lowercase = false;</span></li><li style=\"\" class=\"li2\"><span style=\"color: #ff0000;\">$safeurl->whole_word = false;</span></li><li style=\"\" class=\"li1\"> </li><li style=\"\" class=\"li2\"><span style=\"color: #ff0000;\">$tests = array( </span></li><li style=\"\" class=\"li1\"><span style=\"color: #ff0000;\"> '</span>i\span style=\"color: #ff0000;\">'m a test string!! do u like me. or not......., billy bob!!@#'</span>, </li><li style=\"\" class=\"li2\"> <span style=\"color: #ff0000;\">'<b>some HTML</b> in <i>here</i>!!~'</span>, </li><li style=\"\" class=\"li1\"> <span style=\"color: #ff0000;\">'i!@#*#@ l#*(*(#**$*o**(*^v^*(e d//////e<span style=\"color: #000099; font-weight: bold;\">\\</span><span style=\"color: #000099; font-weight: bold;\">\\</span><span style=\"color: #000099; font-weight: bold;\">\\</span><span style=\"color: #000099; font-weight: bold;\">\\</span>v,,,,,,,,,,n%$#@!~e*(+=t'</span>,</li><li style=\"\" class=\"li2\"> <span style=\"color: #ff0000;\">'A lOng String wiTh a buNchess of words thats! should be -chopped- at the last whole word'</span></li><li style=\"\" class=\"li1\"><span style=\"color: #66cc66;\">)</span>; </li><li style=\"\" class=\"li2\"> </li><li style=\"\" class=\"li1\"><a href=\"http://www.php.net/foreach\"><span style=\"color: #b1b100;\">foreach</span></a><span style=\"color: #66cc66;\">(</span><span style=\"color: #0000ff;\">$tests</span> <a href=\"http://www.php.net/as\"><span style=\"color: #b1b100;\">AS</span></a> <span style=\"color: #0000ff;\">$test</span><span style=\"color: #66cc66;\">)</span> </li><li style=\"\" class=\"li2\"><span style=\"color: #66cc66;\">{</span> </li><li style=\"\" class=\"li1\"> <a href=\"http://www.php.net/echo\"><span style=\"color: #b1b100;\">echo</span></a> <span style=\"color: #0000ff;\">$safeurl</span>-><span style=\"color: #006600;\">make_safe_url</span><span style=\"color: #66cc66;\">(</span><span style=\"color: #0000ff;\">$test</span><span style=\"color: #66cc66;\">)</span>.<span style=\"color: #ff0000;\">'<br />'</span>; </li><li style=\"\" class=\"li2\"><span style=\"color: #66cc66;\">}</span></li><li style=\"\" class=\"li1\"><span style=\"color: #66cc66;\">[</span>/php<span style=\"color: #66cc66;\">]</span></li><li style=\"\" class=\"li2\"> </li><li style=\"\" class=\"li1\"><span style=\"color: #66cc66;\">[</span>b<span style=\"color: #66cc66;\">]</span><span style=\"color: #000000; font-weight: bold;\">Output</span> <span style=\"color: #cc66cc;\">2</span><span style=\"color: #66cc66;\">[</span>/b<span style=\"color: #66cc66;\">]</span></li><li style=\"\" class=\"li2\"><span style=\"color: #66cc66;\">[</span>code<span style=\"color: #66cc66;\">]</span>im-a-test-string-do-u-like-me-or-not-billy-bob</li><li style=\"\" class=\"li1\">some-HTML-in-here</li><li style=\"\" class=\"li2\">i-love-devnet</li><li style=\"\" class=\"li1\">A-lOng-String-wiTh-a-buNchess-of-words-thats-shoul</li></ol></div>Real World Project Usage
Code: Select all
echo '<a href="blog/12/'.$safeurl->make_safe_url($blog_title).'">'.$blog_title.'</a>';