TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-27-2008, 11:45 AM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default Best way to do se friendly urls?

Hey guys! I've got a little question.

I want to convert a title string into a link friendly string.

Example: Site goes live!

Needs to become: site-goes-live

I know I can do string to lowercase and ' ', '-' replace, but what else can I add for weird symbols like é etc... Whats the easiest way to go round this?

Thanks!
Sam Granger is offline  
Reply With Quote
Old 01-27-2008, 12:05 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

I'd go with replacing the special characters with the normal characters. Perhaps using a map table would be the easiest way to accomplish this. I've done so for a romanian website, where I've replaced the diacritics with the associated alphabet letters.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-27-2008, 12:10 PM   #3 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

Hmm, ok. Also, just found urlencode - does that do it for me automatically? Will give it a shot now myself :)
Sam Granger is offline  
Reply With Quote
Old 01-27-2008, 01:06 PM   #4 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

I used this for a project a while ago, worked pretty good:
PHP Code:
function make_url($url) {
    return 
preg_replace('/[\W]/''_'strtolower($url));

sjaq is offline  
Reply With Quote
Old 01-27-2008, 03:10 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This is the code I would use to do it. However, although you can calculate the identifier on-the-fly, if you introduce any more characters into it then consider storing it in the database so you can be certain it can be reversed.

php Code:
class String
{
    /* Bring on namespaces! */
    public static function toSef($szText)
    {
        $szText = str_replace(' ', '-', $szText);
        return preg_replace('~[^-a-z0-9]~i', '', $szText);
    }
}

echo String::toSef('this is a "test"');
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-27-2008, 03:52 PM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

The main idea (I think) would be that of keeping the URLs readable and syntactically correct.

I have attached the code, because the editor automatically converts html entities. It needs a little bit of refactoring, but it does the job.
Attached Files
File Type: txt filter.txt (792 Bytes, 150 views)
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-27-2008, 11:53 PM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Mod_rewrite helps
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-28-2008, 12:31 AM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Sam Granger View Post
I know I can do string to lowercase and ' ', '-' replace, but what else can I add for weird symbols like é etc... Whats the easiest way to go round this?
The easiest way (that I can think of right now) to deal with accented characters would be to build some sort of translation table which specifies which ASCII character any given accented character should be converted to. For example, array('à' => 'a', 'é' => 'e' ... 'Ź' => 'Z'). There might be a lot of characters to consider but you'd only have to construct the translation table once.
Salathe is offline  
Reply With Quote
Old 01-28-2008, 08:51 PM   #9 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Hi Sam,

I came across this post a few minutes ago which may help - some useful comments on it as well about unicode character sets.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:47 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design