Forum Moderators: coopster

Message Too Old, No Replies

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

         

xbl01234

2:42 am on Aug 27, 2007 (gmt 0)

10+ Year Member



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.example.com/test%20%20test...%20test/%2044/

i try to have result like the following;
http://www.example.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>

[edited by: eelixduppy at 3:06 am (utc) on Aug. 27, 2007]
[edit reason] changed to example.com [/edit]

eelixduppy

4:07 am on Aug 27, 2007 (gmt 0)



The problem is that the %20's aren't coming from the POSTed information but rather after the meta refresh takes place the browser is automatically placing them there.

[edited by: eelixduppy at 12:25 pm (utc) on Aug. 27, 2007]

Habtom

4:57 am on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this is where you got it wrong:
$subject=trim($subject);
$subject=str_ireplace("%20","-",$subject);

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

%20 is on URLs when you have space on your URL. You don't replace the "%20", you replace your spaces. This is how it should be:

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

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

Habtom

xbl01234

6:25 am on Aug 27, 2007 (gmt 0)

10+ Year Member




%20 is on URLs when you have space on your URL. You don't replace the "%20", you replace your spaces. This is how it should be:

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

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

It give me the following result after i rewrite as above;
http://www.example.com/xsd-----dd/%2056/

do you known how to get the following format;
http://www.example.com/xsd-dd/56/

[edited by: dreamcatcher at 7:41 am (utc) on Aug. 27, 2007]
[edit reason] Use example.com, thanks. [/edit]

Habtom

6:32 am on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace the relevant lines

$subject=trim($subject);
//The following replaces two spaces with one.
// I think at the end you get only one space no matter how many spaces you have had before.
$subject = str_replace(" ", " ", $subject);

Habtom

Habtom

6:41 am on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



. . . I think the spaces were removed when posted.

$subject = str_replace("[Two Spaces]", "[One Space]", $subject);

xbl01234

8:11 am on Aug 27, 2007 (gmt 0)

10+ Year Member



it does not works.

Habtom

8:19 am on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> it does not works.

That helps a little :)

Did you get the same output?
What is your current code?

This helps a lot now? :)

xbl01234

8:54 am on Aug 27, 2007 (gmt 0)

10+ Year Member



Replace the relevant lines

$subject=trim($subject);
//The following replaces two spaces with one.
// I think at the end you get only one space no matter how many spaces you have had before.
$subject = str_replace(" ", " ", $subject);

i rewrite as following;

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

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

and i get the result from brower as following;
http://example.com/sds---ffxdf/%2062/

but i want the final result as following;
http://example.com/sds-ffxdf/62/

[edited by: eelixduppy at 12:24 pm (utc) on Aug. 27, 2007]

xbl01234

9:00 am on Aug 27, 2007 (gmt 0)

10+ Year Member




. . . I think the spaces were removed when posted.
$subject = str_replace("[Two Spaces]", "[One Space]", $subject);

My code rewrite as following;

$subject=trim($subject);
$subject = str_replace("[Two Spaces]", "[One Space]", $subject);

$id=trim($id);
$subject = str_replace("[Two Spaces]", "[One Space]", $subject);

i have the result from browser as following;
http://example.com/sd%20%20%20ssrf/%2064/

[edited by: eelixduppy at 12:24 pm (utc) on Aug. 27, 2007]

Habtom

9:05 am on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check this function:

function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
}

origianally posted at:

[webmasterworld.com...]

Hab

nedloh

8:33 am on Aug 28, 2007 (gmt 0)

10+ Year Member



urlencode($linkhere)

That should work

xbl01234

10:27 am on Aug 28, 2007 (gmt 0)

10+ Year Member



Hi Habtom;


function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
}//function makeURLFriendly($input){
echo $input = ' My Directory & Stuff ';
echo '<br />';
echo makeURLFriendly($input); // my_directory_stuff

Your one seems work, but it just got a litter problem for now.


when my input is the "$input = ' My Directory & Stuff '", it give me the result is "my directory_stuff". not the "my_directory_stuff".

and i want to ask you what does the following two lines do?
1) $output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
2) $output = preg_replace('/\s\s+/', '_', $output);

xbl01234

10:35 am on Aug 28, 2007 (gmt 0)

10+ Year Member



Hi nedloh:

urlencode($linkhere)
That should work

it does not work, it give me the result as following;

++My+Directory+++++++%26+++Stuff+
<? php
$input = ' My Directory & Stuff ';
echo urlencode($input);
?>

the following format is what i want to be.
My_Directory_Stuff

nedloh

11:08 am on Aug 28, 2007 (gmt 0)

10+ Year Member



1) $output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
2) $output = preg_replace('/\s\s+/', '_', $output);
Those 2 lines mean get the output(lol) then preg_replace which is a function in PHP and it will replace those things with ""So try this function instead"
function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace(" ", "_", $output)
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
"
Should work

xbl01234

8:36 am on Aug 29, 2007 (gmt 0)

10+ Year Member



Hi Nedloh;

function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace(" ", "_", $output);
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
}

The compiler complains give me the following massage;

Warning: preg_replace() [function.preg-replace]: Empty regular expression

for the line;

"$output = preg_replace(" ", "_", $output)"

xbl01234

9:40 am on Aug 29, 2007 (gmt 0)

10+ Year Member



Hi nedloh;
i think using the regular expression would be better.

1) replace all the characters(without [a-z]) from the string by using space.
2) trim($string)
3) replace the spaces by "_".

But the 1), i think using regular expression would be better. But i forget how to use the regular expression, i am going to read then now.