View Full Version : how to get rid off the sign of "%20" from a string
kamkam
08-27-2007, 03:31 AM
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%20%20%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>
Fumigator
08-27-2007, 04:04 AM
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 (urldecode()) and then apply str_replace, replacing " " with "-".
kamkam
08-27-2007, 05:42 AM
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/
_Aerospace_Eng_
08-27-2007, 07:14 AM
I just did exactly what you did and it decodes the %20 into spaces and then replaces the spaces with a hyphen.
<?php
$subject = "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
$subject = urldecode($subject);
$subject = str_replace(" ","-",$subject);
echo $subject;
?>
kamkam
08-27-2007, 08:44 AM
I just did exactly what you did and it decodes the %20 into spaces and then replaces the spaces with a hyphen.
<?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/
Inigoesdr
08-27-2007, 04:29 PM
$subject=urlencode($subject);
$subject=str_replace(" ","-",$subject);
First of all, he said urldecode().
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..
rafiki
08-27-2007, 09:47 PM
you could jus
$str = "http://www.mysite.com/test%20%20test%20%20%20test/%2044/";
$str = str_replace('%20%20', '-');
$str = trim($str);
echo $str;
Inigoesdr
08-27-2007, 10:02 PM
you could jus
$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.
$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.
rafiki
08-28-2007, 02:42 AM
it would replace double spaces with - and get rid of normal spaces....
kamkam
08-28-2007, 11:38 AM
Thanks for your time, i will try your idea tomorrow.
Today, i am busy for my work.
Inigoesdr
08-28-2007, 03:03 PM
and get rid of normal spaces....
Only at the ends of the string, not in the middle of it.
rafiki
08-28-2007, 03:32 PM
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
kamkam
08-29-2007, 10:15 AM
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.
kamkam
08-29-2007, 10:17 AM
you could jus
$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.
kamkam
08-29-2007, 10:27 AM
i think i may go to try regular expression.
aedrin
08-29-2007, 04:48 PM
Regular Expressions won't help you.
Inigoesdr already gave you a solution/things to figure out.
kamkam
08-30-2007, 10:15 AM
Thanks aedrin;
i will try Inigoesdr solution again in weekend, i need to finish my work first, otherwise, my boss will kill me.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.