0

我想为我的文章获得独特的蛞蝓。我正在使用代码点火器。我想知道是否sample-title-1sample-title-2两篇文章具有相同的标题,如 codeignier 对文件上传所做的filename(:num)。我想不出办法。我不是codeigniter的专家。我正在学习它。

我准备了一个函数,当传递一个字符串时$str,它会检查 slug 是否存在,如果存在,它将ID那篇文章的

它工作正常并服务于独特的蛞蝓。但我想要的是有类似sample-title-1and的东西sample-title-2。有什么办法吗?

$data['slug'] = $this->get_slug($data['title']);


public function get_slug ($str)
    {
        $slug = url_title($str, 'dash', true);
        // Do NOT validate if slug already exists
        // UNLESS it's the slug for the current page

        $id = $this->uri->segment(4);
        $this->db->where('slug', $slug);
        ! $id || $this->db->where('id !=', $id);
        $category = $this->category_m->get();

        if (count($category)) {
            return $slug.$id;
        }

        return $slug;
    }
4

4 回答 4

1

易于使用并且对创建独特的 slug 非常有帮助 看看CI slug 库

阅读它的文档来实现它。

于 2013-07-30T23:31:52.587 回答
0

我认为你需要这样的东西:

//Database loaded
//Text helper loaded

function post_uniq_slug($slug, $separator='-', $increment_number_at_end=FALSE) {    
    //check if the last char is a number
    //that could break this script if we don't handle it
    $last_char_is_number = is_numeric($slug[strlen($slug)-1]);
    //add a point to this slug if needed to prevent number collision..
    $slug = $slug. ($last_char_is_number && $increment_number_at_end? '.':'');

    //if slug exists already, increment it
    $i=0;
    $limit = 20; //for security reason
    while( get_instance()->db->where('slug', $slug)->count_all_results('posts') != 0) {
        //increment the slug
        $slug = increment_string($slug, $separator);

        if($i > $limit) {
            //break;
            return FALSE;
        }

        $i++;
    }

    //so now we have unique slug
    //remove the dot create because number collision
    if($last_char_is_number && $increment_number_at_end) $slug = str_replace('.','', $slug);

    return $slug;
}

例子:

post_uniq_slug('sample'); //"sample" exists
//sample-1

post_uniq_slug('sample-2013'); //"sample-2013" exists
//sample-2013-2

post_uniq_slug('sample-2013', '-', TRUE); //increment "sample-2013"
//sample-2014

*未经测试

于 2013-07-31T10:06:32.940 回答
0

我以前做的是做slug db field UNIQUE

然后使用 CI 助手Url HelperText Helper轻松完成所有工作

    $last_id_inserted = //get from db the last post's ID;
    $post_title = "My slug would be";
    $slug =  mb_strtolower(url_title(convert_accented_characters($post_title))).'-'.$last_id_inserted;
    echo $slug;
    //outputting my-slug-would-be-123



//insert the new post with slug

所以 ID 也将是独一无二的。

于 2013-07-31T09:26:06.160 回答
0
public function create_slug($name)
{
 $table='tradeshow';    //Write table name
 $field='slug';         //Write field name
 $slug = $name;  //Write title for slug
 $slug = url_title($name);
 $key=NULL;
 $value=NULL;       
 $i = 0;
 $params = array ();
 $params[$field] = $slug;

if($key)$params["$key !="] = $value;

while ($this->db->from($table)->where($params)->get()->num_rows())
    {  
        if (!preg_match ('/-{1}[0-9]+$/', $slug ))
        $slug .= '-' . ++$i;
        else
        $slug = preg_replace ('/[0-9]+$/', ++$i, $slug );
        $params [$field] = $slug;
    }  

    return $alias=$slug;}
于 2017-09-13T07:28:14.147 回答