Welcome Guest, Not a member yet? Register   Sign In
Just a quick share! for slug users
#1

[eluser]MikeHibbert[/eluser]
Hi guys

I made a function that you might like to have in your plugins/helpers so I thought I would post it for you here seeing as its just a little one!

It a function to create a web freindly URI or slug for anyone who wants to havereferenceds to content in their urls, (you know SEO freindly).

Code:
<?php

function CreateSlug($string){
    $slug = trim($string); // trim the string
    $slug= preg_replace('/[^a-zA-Z0-9 -]/','',$slug );
    $slug= str_replace(' ','-', $slug); // replace spaces by dashes
    $slug= strtolower($slug); // make it lowercase
    return $slug;
}

?>

This will supply a uri like so:

Quote:a string like : "cheese is nice"

will make a uri of : cheese-is-nice

a string of: "CHeese IS nice!"

will make a uri of: cheese-is-nice

Hope this comes in handy!

Mike
#2

[eluser]xwero[/eluser]
[miniRant]It's not because English is the only language without accented characters all other languages should suffer.[/miniRant]

I don't see the benefit of using this function over the url_title function? 1.7.1 has a lowercase option if that was the reason why you created this function.
#3

[eluser]Johan André[/eluser]
Nice!
But what's wrong with the url_title() from the url-helper included in CI?
#4

[eluser]MikeHibbert[/eluser]
erm.... lol!

I dunno really haha!

does it remove none valid characters too?

Ah well we all have that moment in our lives when we realize we are getting old and senile!

Mike
#5

[eluser]Fero[/eluser]
indeed, uri system is ignoring the non-valid characters.. you can type question mark "?" to URL but system won't eat it.. Smile
#6

[eluser]invision[/eluser]
Cool script Smile

Just wondering would you have to put in some checking to make sure the slug doesn't already exist, before doing an INSERT query?
#7

[eluser]MikeHibbert[/eluser]
Well on small sites that shouldn't be a problem as you would probably only have one page called "about us" etc.

But if your going to have a site where you might have duplicate page titles then yeah you would need a bit of checking first.

Even on large sites I find that the titles of page doesn't often collide though.

Mike
#8

[eluser]invision[/eluser]
Cool.
Yep, I was just throwing it in there Smile

I'm planning to do something similar for a news/blog system.


Thanks again.
#9

[eluser]MikeHibbert[/eluser]
YW

Let me know when its done as I love to see others work

Mike
#10

[eluser]dbashyal[/eluser]
[quote author="Johan André" date="1237311450"]Nice!
But what's wrong with the url_title() from the url-helper included in CI?[/quote]

haha i had no idea too that it existed so i end up making this function to achieve that result for my CMS codefight.:

Code:
/*
     * Create and return clean url segment
     */
    function link_clean($segment = false)
    {
        /*
         * START: Clean Segment
         */
        $segment = preg_replace('|[^a-z0-9]+|i','-',strtolower($segment));
        
        //remove last dashes if any
        while(substr($segment, -1) == '-') {
            $segment = substr($segment, 0, -1);
        }
        //remove first dashes if any
        while(substr($segment, 0, 1) == '-') {
            $segment = substr($segment, 1);
        }
        /*END: Clean Segment*/
        
        return $segment;        
    }

But the question is which is faster Smile




Theme © iAndrew 2016 - Forum software by © MyBB