Now each part of the address url put in url_parts array
How can I continue to build a Pretty URLs in php+pdo.
For example database with three table (settings - categories - posts).
Rather than exploding the path, instead match it against a regular expression.
if (preg_match('#^/([^/]+)/(\d+)/([^/]+)/(\d+)/([^/]+)/(\d+)$#', $path, $matches))
{
// $matches[1] will be the setting title
// $matches[2] will be the setting id
// $matches[3] will be the category title
// $matches[4] will be the category id
// $matches[5] will be the post title
// $matches[6] will be the post id
}
elseif (preg_match('#^/([^/]+)/([^/]+)/([^/]+)$#', $path, $matches))
{
// $matches[1] will be the setting title
// $matches[2] will be the category title
// $matches[3] will be the post title
}
Now works in two modes, Only if 3 or 6 slashes in the URL and only responds for posts
What if we’re in the settings or categories url? mysite.com/settings/1 ? mysite.com/settings/1/categories/2 ?
preg_match here set for posts level only
if (preg_match('#^/(\d+)/([^/]+)/(\d+)/([^/]+)/(\d+)/([^/]+)$#', $path, $matches))
{
// $matches[1] will be the setting id
// $matches[2] will be the setting title
// $matches[3] will be the category id
// $matches[4] will be the category title
// $matches[5] will be the post id
// $matches[6] will be the post title
$set_id = "$matches[1]";
$set_title = "$matches[2]";
$cat_id = "$matches[3]";
$cat_title = "$matches[4]";
$post_id = "$matches[5]";
$post_title = "$matches[6]";
if (isset($post_id)) {
include ('posts.php');
} elseif (isset($cat_id)) {
include ('categories.php');
} elseif (isset($set_id)) {
include ('settings.php');
}
}
elseif (preg_match('#^/(\d+)/(\d+)/(\d+)$#', $path, $matches))
{
// $matches[1] will be the setting id
// $matches[2] will be the category id
// $matches[3] will be the post id
$set_id = "$matches[1]";
$cat_id = "$matches[2]";
$post_id = "$matches[3]";
if (isset($post_id)) {
include('posts.php');
} elseif (isset($cat_id)) {
include('categories.php');
} elseif (isset($set_id)) {
include('settings.php');
}
}else{
include 'index.php';
}