not sure what to search for

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

not sure what to search for

Post 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.
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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

urlencode() encodes data for use in a url... is that what you need?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
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:

Post 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.
*/
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
Last edited by aaronhall on Wed Sep 20, 2006 6:07 am, edited 1 time in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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)
Post Reply