Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    how to get rid off the sign of "%20" from a string

    Hello;
    how to get rid off the sign of "%20" from a string?
    I am trying to using the automatic redirect the url. but the result always come with the sign of "%20", could anyone help please.

    the following is my result from brower.
    http://www.mysite.com/test%20%20test...%20test/%2044/

    i try to have result like the following;
    http://www.mysite.com/test-test-test/2044/

    my code as following:
    <?php
    if(isset($_POST['subject'])){
    $subject=$_POST['subject'];
    $id=$_POST['id'];
    }

    $subject=trim($subject);
    $subject=str_ireplace("%20","-",$subject);

    $id=trim($id);
    $id=str_ireplace("%20","",$id);

    ?>

    <html><head>
    <meta http-equiv="Refresh" content="0; url=/<?php echo $subject ?>/ <?php echo $id ?>/">
    </head><body>
    Please follow <a href="/<?php echo $subject ?>/ <?php echo $id ?>/">link</a>!
    </body></html>
    Last edited by kamkam; 08-27-2007 at 03:34 AM. Reason: miss some word

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    THat %20 is actually a urlencoded space. So a string of "hello world" gets encoded to "hello%20world". You can turn it back into a space using http://us.php.net/manual/en/function.urldecode.php and then apply str_replace, replacing " " with "-".

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    i tried rewrite like as you said as following;

    $subject=urlencode($subject);
    $subject=str_replace(" ","-",$subject);


    but it give me the result with the "+" sign instead of the "%20" sign as following;
    http://www.mysite.com/test++++test+++%20test/+++44/

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I just did exactly what you did and it decodes the %20 into spaces and then replaces the spaces with a hyphen.
    PHP Code:
    <?php
    $subject 
    "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
    $subject urldecode($subject);
    $subject str_replace(" ","-",$subject);
    echo 
    $subject;
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    I just did exactly what you did and it decodes the %20 into spaces and then replaces the spaces with a hyphen.
    PHP Code:
    <?php
    $subject 
    "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
    $subject urldecode($subject);
    $subject str_replace(" ","-",$subject);
    echo 
    $subject;
    ?>
    your result give me the following, it still not what i want.

    http://www.mysite.com/test--test---test/-44/

    i want the format as following, it does not matter how many spaces between variables, i just need one "-" of the sign. and the id number just like 44, not /-44/.
    http://www.mysite.com/test-test-test/44/

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by kamkam View Post
    $subject=urlencode($subject);
    $subject=str_replace(" ","-",$subject);
    First of all, he said urldecode().


    Quote Originally Posted by kamkam View Post
    your result give me the following, it still not what i want.

    http://www.mysite.com/test--test---test/-44/

    i want the format as following, it does not matter how many spaces between variables, i just need one "-" of the sign. and the id number just like 44, not /-44/.
    http://www.mysite.com/test-test-test/44/
    And that is happening because you have a space before the ID. You can remove multiple spaces with regular expressions, but you need to figure out why there is a space before the ID to begin with and remove it..

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    you could jus
    PHP Code:
    $str "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
    $str str_replace('%20%20''-');
    $str trim($str);
    echo 
    $str

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by rafiki View Post
    you could jus
    PHP Code:
    $str "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
    $str str_replace('%20%20''-');
    $str trim($str);
    echo 
    $str
    That won't fix the ID, or the third %20 in the middle.

    PHP Code:
    $str "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
    $str str_replace('%20%20''-'$str);
    $str str_replace('%20'''$str);
    echo 
    $str
    Would format this particular string properly, but again, it won't fix why there are seemingly random spaces in the url to begin with.

  • #9
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    it would replace double spaces with - and get rid of normal spaces....

  • #10
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks for your time, i will try your idea tomorrow.

    Today, i am busy for my work.

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by rafiki View Post
    and get rid of normal spaces....
    Only at the ends of the string, not in the middle of it.

  • #12
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    why not in the middle? i see i checked the php manual i always thought it took all whitespace :S god knows where i got that from

  • #13
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    First of all, he said urldecode().



    And that is happening because you have a space before the ID. You can remove multiple spaces with regular expressions, but you need to figure out why there is a space before the ID to begin with and remove it..
    Yes, you are right, i have a space before the id.

  • #14
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rafiki View Post
    you could jus
    PHP Code:
    $str "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
    $str str_replace('%20%20''-');
    $str trim($str);
    echo 
    $str
    This solution just suit for two spaces, not for more than two spaces.

  • #15
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    i think i may go to try regular expression.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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