Changing Urls via PHP?

Discussion in 'PHP' started by scm22ri, Nov 1, 2012.

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


    (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!


    [QUOTE]<?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>';
                }
            }
        }
    }
    ?>
    [/QUOTE]
    PHP:
    [QUOTE]RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/(.*) /auto/cars.php?s=$1&u=$2 [L,NC]
    [/QUOTE]
    PHP:
     
    scm22ri, Nov 1, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    <?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']));
    echo $city;
    exit;
    // $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>';
    }
    }
    }
    } else {
    echo '$_GET[\'u\'] isn\'t set';

    ?>
    What shows on your browser when you run that?
     
    Rukbat, Nov 1, 2012 IP
  3. scm22ri

    scm22ri Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    Hey Rukbat,

    I appreciate the response but I'm getting a parser error.

    Parse error: syntax error, unexpected $end
     
    scm22ri, Nov 1, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Uhm... URLDecode would turn those %20 into regular spaces, so your str_replace would never actualy fire. That's what URLDecode DOES. No %20 or + would make it past that function as it decodes both % values and +

    If you want to turn spaces into hyphens, you should use:
    $city = str_replace(' ','-',urldecode($_GET['u']));

    Though really server side I would just use the spaces in the database rather than filtering them out just to need to filter them again on output... just do the urldecode and work with that.

    Much less your preg_match would never allow values with hyphens past it... or are you just trying to make them spaces not hyphens?

    Gah, can't make sense out of that at all.
     
    deathshadow, Nov 1, 2012 IP
  5. scm22ri

    scm22ri Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #5
    Thanks Deathshadow,

    It's now working!
     
    scm22ri, Nov 1, 2012 IP
  6. smithjoy416

    smithjoy416 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am also appreciate with you.
     
    smithjoy416, Nov 2, 2012 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    That's a missing ] or } or ). The parser is looking for the closing bracket and runs off the end of the file. If you use an editor that highlights matching brackets, it makes it easier to find the missing one. And an editor that adds the closing one when you open one makes it impossible to miss one.
     
    Rukbat, Nov 2, 2012 IP