CRM Hosting


Add to Technorati Favorites

Codeigniter slugify library

A simple Codeigniter library to “slugify” your URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
function slugify($text)
{		
	$CI =& get_instance();
	$CI->load->model('my_model', 'Mod');
	$text = preg_replace('/\W+/', '-', $text);
	$text = strtolower(trim($text, '-'));
	$slug = $text."-1";
	//tests if there is any slug like "text"
	$res = $CI->Mod->getSlugs($text);
	if ($res->num_rows()==0)
	{
		//if not, make it first
		$slug = $text."-1";
	}
	else 
	{
		//else add 1 to number the slug ends with
		$lastItem = $res->row();
		$tmp = explode('-',$lastItem->short);
		$slug = $text . '-' . (int)($tmp[count($tmp)-1]+1);
	}
 
	return $slug;
}

The model looks like:

1
2
3
4
5
6
7
8
9
10
<?php
function getSlugs($text)
	{
		$this->db->order_by('id DESC');
		$this->db->like('short',$text,'after');
		$this->db->limit('1');
		$res = $this->db->get('table_name');
		return $res;
	}
?>



You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button
Comments are DoFollow, so you may consider writing a small note :)

2 Responses to “Codeigniter slugify library”

  1. Great Tipp. Thanks a lot for sharing!

  2. Thank you for the informations you have provided us. We are looking forward to share of the information with you..

Leave a Reply



BRDTracker