I have a blog and when I put the title variable from the article in <title>$title</title> I get mysite.com/article.php?id=7&title=VNC Remote control for iPhone, iTouch PHP: with %20 for space, and what I want is to have my title like mysite.com/VNC-Remote-control-for-iPhone,iTouch. Can someone please tell me how to do it? This is the code that shows the title <?php include('mysql_connect.php'); if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { $id = $_GET['id']; } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { $id = $_POST['id']; } else { echo 'Please choose a news post to view.'; exit(); } $query = "SELECT title FROM news_posts WHERE id='$id' "; $result = @mysql_query($query); if ($result) { $row = mysql_fetch_array($result, MYSQL_ASSOC); $title = $row['title']; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title><?php echo $title; ?> </title> PHP: and this is the code from body: <body> <div id="content"> <?php if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { $id = $_GET['id']; } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { $id = $_POST['id']; } else { echo 'Please choose a news post to view.'; exit(); } $query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'"; $result = @mysql_query($query); if ($result) { $row = mysql_fetch_array($result, MYSQL_ASSOC); $mess = stripslashes($row['post']); $mess = stripslashes($mess); $mess = preg_replace("/rn/i"," ","$mess"); echo '<h2>'.$row['title'] . '</h2><p class="blog_text"> Posted by : <b>' . $row['author'] . '</b><br /> on ' . $row['date'] . '<br /><br />' . $mess . '<br /><hr /></p>'; include("comments.php"); } ?> </div></body> PHP:
If it is only a space you're dealing with a str_replace should work fine. $new_title = str_replace(' ','-',$title); PHP:
You're going to have to replace all non alpha numberic characters as well such as commas, quotes and symbols. You should be able to do this in 1 line with a regex or preg_replace. Search around on Google for PHP change titles for URLs and you should find someone who has already done this. SEO scripts for applications like Wordpress and Oscommerce are already doing exactly what you're looking for so someone should have some code somewhere. Failing this - it's something I would be able to script for you for a fee.
To replace non-alphanumeric characters, a simple reg ex will do it. If you're wanting the whole article.php?title=blah Code (markup): to just be /blah Code (markup): you have to use something like mod_rewrite.
Yes, you get urlencoded string. You must decode it & pass to htmlspacialchars func. and that's all And yes, as @omglol said, mod_rewrite is good practice.