not sure what to search for
Moderator: General Moderators
not sure what to search for
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.
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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
urlencode() encodes data for use in a url... is that what you need?
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.
I'll give it a shot.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Hmm, easier than I thought.
I tested it on this.
It returns
Crappy example. Anyone want to try to throw some things at it?
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);
}Code: Select all
$str = '~~~~WHO IZ DA LIKE\'S BEST IN DA NFL & europe !!??@?!@@/';
echo make_safe_url($str);Code: Select all
who-iz-da-likes-best-in-da-nfl-and-europe-Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
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.
*/-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
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]- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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
http://www.evilwalrus.org/scripts/Conve ... dly_titles
Last edited by aaronhall on Wed Sep 20, 2006 6:07 am, edited 1 time in total.