用PHP生成独特的url slug

时间:2015-09-22 15:22:32

标签: php mysql

我正在开发一个小型CMS系统。目前在试图生成一个独特的网址时脑死亡。我从页面标题生成url。有一个很好的脚本来实现这一点,但我无法解决重复问题。

类似问题here但获得确切的输出。

我设法做了以下事情;

this-is-the-slug
this-is-the-slug-2

但如果我第三次创建相同的帖子,它只会复制:this-is-the-slug-2

$i = 1;
$baseurl = $url;
//$check database here
if($thereisamatch){
$url = $baseurl . "-" . $i++;  
}

我无法绕过它,感谢您的帮助。

if(isset($_POST['save'])) {
    $title = mysqli_real_escape_string($db, $_POST['title']);
    $url = toAscii($title);
    // check url
    $content = $_POST['content'];
    $author = $_SESSION['thelab_username'];
    $type = $_POST['type'];
    $showtitle = $_POST['showtitle'];
    $saveas = $_POST['status'];

    $insert = $connection->query(" INSERT INTO lab_pages (title, permalink, content, author, status, type, showtitle)
    VALUES ('$title', '$newslug', '$content', '$author', '$saveas', '$type', '$showtitle') ");

    if($insert === TRUE) {
        echo '<div id="success">Page added. <button id="close">Close</button></div>';
    }

    else {
        echo '<div id="error">Failed. Try again. <button id="failed">Close</button></div>';
        printf("Errormessage: %s\n", $db->error);
    }
}

function toAscii($str, $replace=array(), $delimiter='-') {
 if( !empty($replace) ) {
  $str = str_replace((array)$replace, ' ', $str);
 }

 $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
 $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
 $clean = strtolower(trim($clean, '-'));
 $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

 return $clean;
}

5 个答案:

答案 0 :(得分:3)

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

function get_one_value($con,$query) {
    $result = mysqli_query($con,$query);
    $row = mysqli_fetch_row($result);
    return($row[0]);
}
$slug = slug($input_without_trailing_number);
$exists = get_one_value("select count(id) from table where slug = '$slug'"); 

if ($exists > 0)
{
    $new_number = $exists + 1;
    $newslug = $slug."-".$new_number;
}
echo $newslug;

投入我自己的slug函数,因为我发现它无限有用,我希望其他人也一样。

答案 1 :(得分:1)

我也遇到永久链接问题。特别是当我想从网址中删除俄语,中文和除英语之外的任何其他字符时。尝试下面的功能。我使用的是PDO但没有太大区别。

        function FilterPermalink($link){
            $db = new Connect;
            $link = strtolower(trim($link));
            $link = preg_replace('/[^a-z0-9-]/', '-', $link);
            $link = preg_replace('/-+/', "-", $link);
            $link = rtrim($link, '-');
            $link = preg_replace('/\s+/', '-', $link);
            if(strlen($link) > 30)
                $link = substr($link, 0, 30);
            $existing_lnk = $db->prepare("SELECT id FROM posts WHERE permalink = :permalink");
            $existing_lnk->execute(array('permalink' => $link));
            $num = $existing_lnk->fetchAll(PDO::FETCH_COLUMN);
            $first_total = count($num);
            for($i=0;$first_total != 0;$i++){
                if($i == 0){
                    $new_number = $first_total + 1;
                    $newlink = $link."-".$new_number;
                }
                $check_lnk = $db->prepare("SELECT id FROM posts WHERE permalink = :permalink");
                $check_lnk->execute(array('permalink' => $newlink));
                $other = $check_lnk->fetchAll(PDO::FETCH_COLUMN);
                $other_total = count($other);
                if($other_total != 0){
                    $first_total = $first_total + $other_total;
                    $new_number = $first_total;
                    $newlink = $link."-".$new_number;
                }elseif($other_total == 0){
                    $first_total = 0;
                }           
            }
            if($i > 0)
                return $newlink;
            else
                return $link;
        }

它适用于我。

答案 2 :(得分:0)

使用数据库匹配的值(SELECT COUNT(*))并增加它以获得新的后缀。

PHP脚本不会相互通信,因此$i在开始时始终设置为1,然后增加1,因此2。< / p>

感谢您的源代码。因此,代码的开头应该是:

if(isset($_POST['save'])) {
    $title = mysqli_real_escape_string($db, $_POST['title']);
    $url = toAscii($title);
    // check url
    $content = $_POST['content'];
    $author = $_SESSION['thelab_username'];
    $type = $_POST['type'];
    $showtitle = $_POST['showtitle'];
    $saveas = $_POST['status']; // no modifications until here

    // count entries with permalink like our slug
    $urlmask = $url.'%';
    $sql = 'SELECT COUNT(*) FROM lab_pages WHERE permalink LIKE ?';
    $stst =  $db->stmt_init();
    $stst->bind_param('s', $urlmask);
    $stst->execute();
    $stst->store_result();
    $stst->bind_result($count);
    $stst->fetch();
    $stst->free_result();
    $newslug = sprintf('%s-%d', $url, $count+1);

    $insert = $connection->query(" INSERT INTO lab_pages (title, permalink, content, author, status, type, showtitle)
    VALUES ('$title', '$newslug', '$content', '$author', '$saveas', '$type', '$showtitle') ");
    :

重要的是使用类似SELECT COUNT(*) FROM lab_pages WHERE permalink LIKE "$url%"的SQL语句获取当前slug的数量。然后你可以增加它并创建一个新的独特slug。由于性能,列永久链接应该被编入索引。

但是,我宁愿使用uniqid()这样的东西。

为了演示,我使用了预准备语句,但您当然可以通过普通的query-fetch_array-free_result序列运行查询。

答案 3 :(得分:0)

您可以通过两种方式解决它。

  1. 将检查数据库移入功能并使用循环。
  2. 像这样:

    function check_into_database($url){
        //if isset - return true else false
    }
    $i = 1; $baseurl = $url;
    while(check_into_database($url)){
        $url = $baseurl . "-" . $i++;        
    }
    

    第二种方式

    使用带有count(id)的查询来检查所有网址。

    查询应该是这样的:

    SELECT COUNT(id) FROM tbl_name WHERE url LIKE "$url%"
    

    此查询将返回类似于this-the-the slug%的网址计数。

答案 4 :(得分:0)

尝试使用此代码,这对我有用。

//检查子弹是否已经存在

$post_slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST['post_title'])));

$query = "SELECT * FROM `posts` WHERE post_slug = '$post_slug'";
$result = mysqli_query($conn, $query) or die(mysql_error());
$ifExist = mysqli_num_rows($result);
if ($ifExist >= 1) {

    $post_slug = $post_slug . '-' . $ifExist++;
} else {
    $post_slug = $post_slug;
}
相关问题