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

[eluser]MikeHibbert[/eluser]
[quote author="dbashyal" date="1237829071"][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[/quote]

Is there a point when this might become realtime critical?

As is it seems right that you made your own as we establish above, the built in function doesnt strip out invalid characters.

I hazard a guess that mine is faster than yours as I dont have any while loops in mine. Worth testing though if you have time Big Grin

Mike
#12

[eluser]dbashyal[/eluser]
[quote author="MikeHibbert" date="1237831709"]Is there a point when this might become realtime critical?

As is it seems right that you made your own as we establish above, the built in function doesnt strip out invalid characters.

I hazard a guess that mine is faster than yours as I dont have any while loops in mine. Worth testing though if you have time Big Grin

Mike[/quote]

Yes you are right. I have unnecessary loops, may be below would have been much faster than mine above.

Code:
/*
* Create and return clean url segment
*/
function link_clean($segment = false)
{
    //remove invalid characters with spaces, trim it and replace spaces with dashes
    return preg_replace('|\s\s+|','-',trim(preg_replace('|[^a-z0-9]+|i',' ',strtolower($segment))));
}
#13

[eluser]MikeHibbert[/eluser]
[quote author="dbashyal" date="1237862073"][quote author="MikeHibbert" date="1237831709"]Is there a point when this might become realtime critical?

As is it seems right that you made your own as we establish above, the built in function doesnt strip out invalid characters.

I hazard a guess that mine is faster than yours as I dont have any while loops in mine. Worth testing though if you have time Big Grin

Mike[/quote]

Yes you are right. I have unnecessary loops, may be below would have been much faster than mine above.

Code:
/*
* Create and return clean url segment
*/
function link_clean($segment = false)
{
    //remove invalid characters with spaces, trim it and replace spaces with dashes
    return preg_replace('|\s\s+|','-',trim(preg_replace('|[^a-z0-9]+|i',' ',strtolower($segment))));
}
[/quote]

Hehe there comes a point in an algorithm when its function become meaningless to the human eye, and this humans eye just lost the plot reading that lol!

Joking a side, that looks to be a good one too Big Grin

Mike
#14

[eluser]dbashyal[/eluser]
[quote author="MikeHibbert" date="1237864306"]
Hehe there comes a point in an algorithm when its function become meaningless to the human eye, and this humans eye just lost the plot reading that lol!

Joking a side, that looks to be a good one too Big Grin

Mike[/quote]

This is expanded form for human eyes Smile
Code:
/*
* Create and return clean url segment
*/
function link_clean($segment = false)
{
    //change to lower case.
    $segment = strtolower($segment);
    //remove invalid characters with spaces
    $segment = preg_replace('|[^a-z0-9]+|',' ',$segment);
    //trim it
    $segment = trim($segment);
    //replace spaces with dashes
    $segment = preg_replace('|\s\s+|','-',$segment);
    
    //finally return it
    return $segment;
}




Theme © iAndrew 2016 - Forum software by © MyBB