replacing %20 in query string

Discussion in 'PHP' started by Alffy, Feb 4, 2008.

  1. #1
    hi second question for the board treat me gently :p

    I am working on my football site and am wondering how can I make my urls work better.

    http://www.mysite.com/index.php?team=Liverpool - ok

    http://www.mysite.com/index.php?team=Manchester%20United - bad %20

    Is there any way to remove the %20 or replace with an _ underscore or hyphen?
     
    Alffy, Feb 4, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You can use http://us.php.net/str_replace to replace characters, otherwise http://us3.php.net/urlencode and http://us3.php.net/urldecode are commands specially made for working with URL data.
     
    ToddMicheau, Feb 4, 2008 IP
  3. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if you are printing out a string for a link, you could do something like
    $url = str_replace("%20","_",$org_url);
    this would make all the %20's into underscores

    or you could go a little more advanced and do something like
    $url = str_replace(array("%20"," "),"_",$org_url);
    this would replace a real space, and the %20 at the same time, and if there was other characters you wanted to replace as well , just say a ', you could just add it into the array as well.
     
    powerspike, Feb 4, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Kaizoku, Feb 4, 2008 IP
  5. Alffy

    Alffy Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks for the replies,

    I tried using $team=str_replace(" ","_",$team);
    it works fine on the variable echoed on the output for the page display but not on the url %20 is still there.

    <?php
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query = "SELECT * FROM football WHERE team = '".$_GET['team']."'";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    $i=0;
    while ($i < $num) {
    $team=mysql_result($result,$i,"team");
    $team=str_replace(" ","_",$team);
    $players=mysql_result($result,$i,"players");
    $i++;
    }
    ?>

    Any ideas how I can get this working on the url ?
     
    Alffy, Feb 5, 2008 IP
  6. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #6
    What do you mean? Are you trying to change the URL in the browser window?. . .
     
    ToddMicheau, Feb 5, 2008 IP
  7. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Woah. Red card for putting $_GET data directly into a query. Always filter and clean user input before putting it into your database, displaying it on their browser, or anything else. Do this instead:

    
    $get_team=mysql_real_escape_string(str_replace(" ","_",$_GET['team']););
    $query = "SELECT * FROM football WHERE team = '".$get_team."'";
    
    PHP:
    Also, you can simplify your loop by removing the count variable entirely and using mysql_fetch _assoc(), which is faster than mysql_result() anyway.

    
    <?php
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $get_team=mysql_real_escape_string(str_replace(" ","_",$_GET['team']););
    $query = "SELECT * FROM football WHERE team = '".$get_team."'";
    $result=mysql_query($query);
    
    while ($row=@mysql_fetch_assoc($result)) {
    $team=str_replace(" ","_",$row['team']);
    $players=$row['players'];
    }
    ?>
    
    PHP:
    I doubt the last str_replace() is necessary, but I have no idea what the rest of the code looks like.
     
    The Critic, Feb 5, 2008 IP
  8. WebHostingNerds.com

    WebHostingNerds.com Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    the url depends on how you make your links
    if you use the str_replace function when you create the link you can make it point to Manchester_United, then you use str_replace again to replace _ with ' ' when you access the URL parameters

    there's no way you can change the page URL once it's loaded, you can only change what's clickable by your visitors in order to prevent %20 in the URL
     
    WebHostingNerds.com, Feb 6, 2008 IP