Home » , » Convert strings into slugs in php

Convert strings into slugs in php

Written By Unknown on Senin, 02 Januari 2012 | 02.02

While working with php, at some point we need to convert our strings into slugs for seo friendly urls or some other purpose. And our strings may contain different special characters.

To get rid of that characters and create a slug of that string use this function. It takes the string and return the seo friendly slug. Very simple but efficient.



function slug($string){
$string = strtolower(trim($string));
$string = preg_replace('/[^a-z0-9-]/', '-', $string);
$string = preg_replace('/-+/', "-", $string);
return trim($string,"-");
}


Call function like this

$slug=slug("This is my title");

This is very helpful when we want to create urls based on some strings like create urls from post title.
Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger