%20 in URLs

Discussion in 'Programming' started by johneva, Feb 19, 2010.

  1. #1
    Hi all

    I am looking for the best way to change the %20 in my URL's to a -

    I currently make the URL like this

    echo"<td><a href='further-info/$id/$make/$model' class='no_under'>$make $model</a></td>";
    PHP:
    But as the model field has spaces in it makes a URL like this.

    http://www.my-site.co.uk/dual_control_car_hire/further-info/28/BMW/Mini%20Cooper%20-%201.6%20diesel

    But I want one like
    http://www.my-site.co.uk/dual_control_car_hire/further-info/28/BMW/Mini-Cooper---1.6-diesel

    Cheers
    John
     
    johneva, Feb 19, 2010 IP
  2. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you looking for something like this in php?
    $myurl = str_replace(" ", "-", $myurl);
     
    NeoCambell, Feb 20, 2010 IP
    johneva likes this.
  3. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #3
    NeoCambell is right, you can replace the spaces with hyphens using str_replace. Just make sure you use it before you require it:

    
    // URLify the url
    $makeURL  = str_replace(" ", "-", $make);
    $modelURL = str_replace(" ", "-", $model);
    
    // Print out table cell
    echo"<td><a href='further-info/$id/$makeURL/$modelURL' class='no_under'>$make $model</a></td>";
    
    PHP:
     
    Darkhodge, Feb 20, 2010 IP
    johneva likes this.
  4. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #4
    Cheers guys yeah managed to sort it now.
     
    johneva, Feb 21, 2010 IP
  5. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #5
    Good to hear :)
     
    Darkhodge, Feb 21, 2010 IP