I am creating a function that will return a slug based on a string entered, if a slug exists on database it will append a number with -$num, I am using codeigniter,
When I die the variable it returns the proper slug on _generate_slug it returns the correct value, but when I die on the index function it returns blank;
Here's my function
controllers/test.php
public function index()
{
echo $this->_generate_slug('test');
}
protected function _generate_slug($string,$cntr = 0)
{
if($cntr == 0){
$slug = create_slug($string);
}else{
$slug = create_slug($string).'-'.$cntr;
}
if($this->test_model->slug_exist($slug)){
$cntr++;
$this->_generate_slug($string,$cntr);
}else{
return $slug;
}
}
helpers/test.php
function create_slug($string)
{
$slug = strtolower( $string );
$slug = str_replace('&','and',$slug);
$slug = preg_replace('/[%\'"``]/', '', $slug);
$slug = preg_replace('/[^a-zA-Z0-9-]/','-',$slug);
$slug = preg_replace("/[-]+/", "-", $slug);
$slug = trim($slug, '-');
return $slug;
}