title in address bar

Discussion in 'PHP' started by snowLee, Apr 8, 2009.

  1. #1
    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:

     
    snowLee, Apr 8, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    If it is only a space you're dealing with a str_replace should work fine.

    
    
    $new_title = str_replace(' ','-',$title);
    
    
    PHP:
     
    jestep, Apr 8, 2009 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    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. ;)
     
    Weirfire, Apr 8, 2009 IP
  4. omglol

    omglol Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    omglol, Apr 9, 2009 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    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.
     
    koko5, Apr 9, 2009 IP