Page 1 of 1
not sure what to search for
Posted: Wed Sep 20, 2006 12:18 am
by s.dot
This has probably been asked a million times, but I'm not sure what to search for. I have a bunch of user created quiz titles, that I would like to turn into URLs using a mod_rewrite. The mod rewrite or how to do it isn't the problem.
I need to find a function that will do this for me:
For example, "~~~HOW MUCH DO U LIKE'S DA NFLZ!?!?!?@@" wouldn't go well in a url. I need to find a function that will turn it into suitable characters, like /quiz/how-much-do-you-like-da-nfl.html =]
What do I search for? Any of you know of such functions? I imagine one wouldn't be hard to write with a regex and preg_replace, but I'm not too good with regex.
Posted: Wed Sep 20, 2006 12:26 am
by Luke
urlencode() encodes data for use in a url... is that what you need?
Posted: Wed Sep 20, 2006 1:15 am
by s.dot
Negative. I want it to STRIP all non a-z 0-9 characters, replace & with "and" replace a space with -. Hmm seems like I just defined my own regex. =]
I'll give it a shot.
Posted: Wed Sep 20, 2006 1:26 am
by s.dot
Hmm, easier than I thought.
Code: Select all
function make_safe_url($text)
{
$text = str_replace(array(' ',',','&'),array('-','','and'),$text);
$text = preg_replace("/[^a-z0-9_-]/i",'',$text);
return strtolower($text);
}
I tested it on this.
Code: Select all
$str = '~~~~WHO IZ DA LIKE\'S BEST IN DA NFL & europe !!??@?!@@/';
echo make_safe_url($str);
It returns
Code: Select all
who-iz-da-likes-best-in-da-nfl-and-europe-
Crappy example. Anyone want to try to throw some things at it?
Posted: Wed Sep 20, 2006 2:09 am
by nickvd
Since I've been on a Unit Testing Kick... Getting used to tests first with intentions to edge into tdd sooner or later....
Code: Select all
function cleanUrl($url) {
// $url = preg_replace('/&/',' and ',$url);//switch &'s
// $url = preg_replace('/\s{1,}/','-',$url);//switch spaces
// $url = preg_replace('/[^a-z0-9-]/','',strtolower($url)); //remove the rest
// return $url;
// This works...
// BUT... Would this more or less efficiant? it's not all that hard to decipher,
// although you have to reverse the order of the logic...
return
preg_replace('/[^a-z0-9-]/','',
preg_replace('/\s{1,}/','-',
preg_replace('/&/',' and ',
strtolower($url)
)));
}
class cleanUrlTest extends UnitTestCase {
function testUrlCleaner(){
$test_case = cleanUrl('~~~HOW MUCH DO U LIKE\'S DA NFLZ & OR T3H P0K3R!?!?!?@@');
$this->assertIdentical($test_case,'how-much-do-u-likes-da-nflz-and-or-t3h-p0k3r');
$test_case = cleanUrl('~~~D#@)))E)()#@!(@@v#( & *#*N?!?!e?@t@');
$this->assertIdentical($test_case,'dev-and-net');
$test_case = cleanUrl('This is my url&it is great!');
$this->assertIdentical($test_case,'this-is-my-url-and-it-is-great');
}
}
$test = new cleanUrlTest();
$test->run(new HTMLReporter);
/*
cleanUrlTest
1/1 test cases complete: 3 passes, 0 fails and 0 exceptions.
*/
Posted: Wed Sep 20, 2006 2:14 am
by nickvd
Code: Select all
cleanUrlTest
Fail: testMake_Safe_UrlCleaner -> Identical expectation [String: dev--and--net] fails with [String: dev-and-net] at character 4 with [dev--and--net] and [dev-and-net] at [/var/www/shorty/tests.php line 54]
Fail: testMake_Safe_UrlCleaner -> Identical expectation [String: this-is-my-urlandit-is-great] fails with [String: this-is-my-url-and-it-is-great] at character 14 with [this-is-my-urlandit-is-great] and [this-is-my-url-and-it-is-great] at [/var/www/shorty/tests.php line 57]
Posted: Wed Sep 20, 2006 5:57 am
by aaronhall
The following script imitates Wikipedia- and Digg-style URLs. I think this might be more of what you're looking for.
http://www.evilwalrus.org/scripts/Conve ... dly_titles
Posted: Wed Sep 20, 2006 6:03 am
by aaronhall
I forgot to mention that it's important to double check that the generated URL title isn't a duplicate of another, as it is possible for two different strings to come out of the function exactly the same, matie. (yarr, I hope I didn't miss National Talk Like a Pirate Day)