Creating of Slug from title in PHP is very easy and we can easily do this things. But How can we generate unique url slug from same title in PHP. So, this things we have dicuss here. When we have developed any dynamic ecommerce or content based or any website which general public can access which has been developed PHP then generating or creating of slug is very important task, because we want to generate every url must be unique if it is duplicate then it will load different result on web page.
So, when we have store dynamic website content into Mysql database then at that time it will typical pattern for fetching data and generate unique slug which has been based on particular title. This unique and seo friendly slug will gives not only to users but also search engine can human friendly way to get access website dynamic page from this unique slug. So, Generating or Creating of unique slug is very important task for any PHP web developers.
If we have use any PHP framework for developing website then we can get easily make slug from title and but here we have discuss how to make url slug using PHP and it must be unique. So, in this post we have describe step by step how can we generate unique slug by using PHP. Here we have store slug data in mysql table and when we have generate new slug from particular title then it will check in database particular slug or url already present in our database or not, it is present then it will count how many time it has been appear in database and lastly it will append maximum number plus one number append into slug and make in unique. Below you can find complete source code for how to create unique slug in php.
Source Code
<?php
//header.php
$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
$slug = '';
if(isset($_POST["create"]))
{
//$slug = preg_replace(pattern, replacement, subject)
$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["title"])));
$query = "SELECT slug_url FROM slug WHERE slug_url LIKE '$slug%'";
$statement = $connect->prepare($query);
if($statement->execute())
{
$total_row = $statement->rowCount();
if($total_row > 0)
{
$result = $statement->fetchAll();
foreach($result as $row)
{
$data[] = $row['slug_url'];
}
if(in_array($slug, $data))
{
$count = 0;
while( in_array( ($slug . '-' . ++$count ), $data) );
$slug = $slug . '-' . $count;
}
}
}
$insert_data = array(
':slug_title' => $_POST['title'],
':slug_url' => $slug
);
$query = "INSERT INTO slug (slug_title, slug_url) VALUES (:slug_title, :slug_url)";
$statement = $connect->prepare($query);
$statement->execute($insert_data);
//echo $slug;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>How Create Unique Slug in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<style>
.box
{
max-width:600px;
width:100%;
margin: 0 auto;;
}
</style>
</head>
<body>
<div class="container box">
<br />
<h3 align="center">How Create Unique Slug in PHP</h3>
<br />
<form method="post">
<div class="form-group">
<label>Enter Title for Slug</label>
<input type="text" name="title" class="form-control" required />
</div>
<br />
<div class="form-group">
<input type="submit" name="create" class="btn btn-info" value="Create" />
</div>
<br />
<h4>Generated Slug - <?php echo $slug; ?></h4>
</form>
</div>
</body>
</html>
Database
--
-- Database: `testing`
--
-- --------------------------------------------------------
--
-- Table structure for table `slug`
--
CREATE TABLE `slug` (
`slug_id` int(11) NOT NULL,
`slug_title` varchar(255) NOT NULL,
`slug_url` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `slug`
--
ALTER TABLE `slug`
ADD PRIMARY KEY (`slug_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `slug`
--
ALTER TABLE `slug`
MODIFY `slug_id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
thank you for this tutorial. Please i have a table for post that has a column title for my post title. Since i want to generate a slug URL from my post title, do i have to create a column in my post table and name it tittle-slug?
ReplyDeleteor i should create a different table for slug?