TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Best way to do se friendly urls? (http://www.talkphp.com/general/2113-best-way-do-se-friendly-urls.html)

Sam Granger 01-27-2008 11:45 AM

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!

xenon 01-27-2008 12:05 PM

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.

Sam Granger 01-27-2008 12:10 PM

Hmm, ok. Also, just found urlencode - does that do it for me automatically? Will give it a shot now myself :)

sjaq 01-27-2008 01:06 PM

I used this for a project a while ago, worked pretty good:
PHP Code:

function make_url($url) {
    return 
preg_replace('/[\W]/''_'strtolower($url));



Wildhoney 01-27-2008 03:10 PM

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"');

xenon 01-27-2008 03:52 PM

1 Attachment(s)
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.

Orc 01-27-2008 11:53 PM

Mod_rewrite helps

Salathe 01-28-2008 12:31 AM

Quote:

Originally Posted by Sam Granger (Post 9766)
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. ^^

Alan @ CIT 01-28-2008 08:51 PM

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


All times are GMT. The time now is 12:43 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0