SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing Urls via PHP?

    Hi Everyone,

    I've partly figured out how to change my URLs but I'm having trouble with fixing the space(%20) that's located in my URL.

    I've tried using the str_replace function but it's either not working or I'm not doing in correctly.

    My goal is to turn this URL (first one below this statement) into the URL that's below the first one. I'm having a hard time figuring this out. Any help would be appreciated. Thanks Everyone!

    (URL that works but it has %20, which is something I don't want)
    http://whatsmyowncarworth.com/auto/florida/key%20west

    (URL that does not work but I'm attempting to make the above URL look like this one)
    http://whatsmyowncarworth.com/auto/florida/key-west

    My .htaccess code is below as well. Thanks again everyone!

    PHP Code:
    <?php
    include('init.php'); // connection to database


    // if city exists...
    if (isset($_GET['u'])) {
        
    // $city = str_replace(' ','-');
        // decode and replace hyphen with space
        // $city = str_replace('-','',urldecode($_GET['u']));
        // $city = str_replace('%20','-',urldecode($_GET['u']));
           
    $city str_replace('%20','-',urldecode($_GET['u']));
        
    // $city = str_replace(urldecode($_GET['u']),' ','-');
        // $city = str_replace('','-',urldecode($_GET['u'])); 
        // $city = str_replace('-','','%20','-', urldecode($_GET['u']));
        
        // if value contains only letters, numbers or spaces...
        
    if ( preg_match('~^[a-z0-9 ]+$~i',$city) ) {
            
    // select data from database...
            
    $data mysql_query("SELECT State, City FROM cars WHERE City='$city'" );
            if (
    mysql_num_rows($data) > 0) {
                while (
    $row mysql_fetch_assoc($data)) {
                    echo 
    $row["City"].'<br>'
                    echo 
    $row["State"].'<br>';
                    echo 
    'This is what I get when I echo $_GET '.$_GET['u'].'<br>';
                }
            }
        }
    }
    ?>
    PHP Code:
    RewriteEngine On
    RewriteCond 
    %{REQUEST_FILENAME} !-f
    RewriteCond 
    %{REQUEST_FILENAME} !-d
    RewriteRule 
    ^([^/]+)/(.*) /auto/cars.php?s=$1&u=$[L,NC

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,287
    Mentioned
    182 Post(s)
    Tagged
    4 Thread(s)
    If I understand correctly, you simply need to apply the PHP urldecode() method to the string(s).
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    You're URLDecoding the variable, which changes "Key%20West" into "Key West" and then doing a str-replace on it looking for %20... but the %20 is already gone, so you end up with "Key West" again.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Edit:

    I see StarLion also came to the same conclusion


    PHP Code:
    $city str_replace('%20','-',urldecode($_GET['u'])); 
    Because you are using urldecode(), %20 is no longer valid, %20 is now represented as a space.

    So the following should work:
    PHP Code:
    $city str_replace(' ','-',urldecode($_GET['u'])); 
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Everyone,

    I appreciate all of the responses. Cpradio, thanks for the syntax but it's not working for some reason.

    I tried
    PHP Code:
    $city str_replace(' ','-',urldecode($_GET['u'])); 
    but my page isn't echoing anything.

    http://whatsmyowncarworth.com/auto/florida/key-west

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Have you performed a var_dump($_GET['u']) and on $city to see what data you are receiving?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey cpradio,

    Thanks for the response. In order to show you the var_dump I had to slightly change the syntax

    (it's currently this way to show you the var dump)
    PHP Code:
    $city str_replace('','-',urldecode($_GET['u'])); 
    verses

    PHP Code:
    $city str_replace(' ','-',urldecode($_GET['u'])); 
    To answer your question, the var_dump is echoing out
    string(8) "key west"
    http://whatsmyowncarworth.com/auto/florida/key%20west

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Okay, so what value are you trying to get for your query?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to take the "%20" and turn that into a -. So my URL will look like this

    http://whatsmyowncarworth.com/auto/florida/key-west

    Thanks again cpradio for the help!

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    but nothing in your PHP is building a URL, it is reading from the URL...
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought that's what I had to do in order to make the URLS that I wanted. If you were me, what would you do?

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Your rewrite will handle the format of your URLs
    Code:
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([^/]+)/(.*) /auto/cars.php?s=$1&u=$2 [L,NC]
    That should already be passing key-west to your PHP script, which means, you then need to replace "-" with " " so it can find it in your table.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  13. #13
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks CPradio! It's working! Yippy!

    But let me ask you this, is their easier way to do this?

    Thanks!

  14. #14
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    The removing of the hyphen, or achieving the URL format you now want?

    In either case, no, not really. You are doing it right. The only thing I would mention is that if you ever want to replace multiple characters, to use preg_replace() instead of creating a str_replace for each one (especially if you are going to replace each character with a space).

    But you are using RewriteRules to do what they were intended, granted, @dklynn ; would give you a very long drawn out rant over the use of (.*), as it is a catch all, when you likely could have used ([a-zA-Z0-9 -]+)
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  15. #15
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot cpradio. I appreciate your help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •