xbl1 Posted August 27, 2007 Share Posted August 27, 2007 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: <?phpif(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> Link to comment Share on other sites More sharing options...
user4fun Posted August 27, 2007 Share Posted August 27, 2007 If you are not putting the %20 in your variable yourself, then it looks like your browser is putting it in substitute of a character that you have in itAtleast that is what i think Link to comment Share on other sites More sharing options...
justsomeguy Posted August 27, 2007 Share Posted August 27, 2007 A %20 character is a space, not a dash. If you type spaces into the address bar the browser will automatically convert them to %20, that is what they are supposed to do. A space is not technically a valid character in a URL, so spaces and other special characters that aren't valid need to be encoded with their hex values. You can use the rawurlencode and rawurldecode functions to encode or decode a string. Link to comment Share on other sites More sharing options...
xbl1 Posted August 27, 2007 Author Share Posted August 27, 2007 A %20 character is a space, not a dash. If you type spaces into the address bar the browser will automatically convert them to %20, that is what they are supposed to do. A space is not technically a valid character in a URL, so spaces and other special characters that aren't valid need to be encoded with their hex values. You can use the rawurlencode and rawurldecode functions to encode or decode a string. After i wrote as folllowing; $subject=rawurldecode($subject); $subject=str_replace(" ","-",$subject); $id=rawurldecode($id); it give me the following result; http://mysite.com/df---tt/%2059/ but i want the format as following; http://mysite.com/df-tt/59/ i just want one sign of the "-", it doesn't matter how many spaces between the variables. and i still could not get rid of the %20 from the id. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 27, 2007 Share Posted August 27, 2007 What URL are you starting with? And why is the URL messed up in the first place? Link to comment Share on other sites More sharing options...
xbl1 Posted August 27, 2007 Author Share Posted August 27, 2007 What URL are you starting with? And why is the URL messed up in the first place?>What URL are you starting with?http://businessfind101.com/>And why is the URL messed up in the first place?if user miss type for the subject as following;how to get rid of the something? Link to comment Share on other sites More sharing options...
slipszenko Posted August 27, 2007 Share Posted August 27, 2007 I think it's just because your've got space there in your code. <?phpif(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> I know it doesn't look like much of a difference, but now there is no space inbetween the "/" and the "<?php". Link to comment Share on other sites More sharing options...
xbl1 Posted August 27, 2007 Author Share Posted August 27, 2007 I think it's just because your've got space there in your code.<?phpif(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> I know it doesn't look like much of a difference, but now there is no space inbetween the "/" and the "<?php". yes, you are right, i got a spaces between the "/" and "<?php".you almost solve my problem now.at the movement, it give me the result as following, after i copied your code.http://businessfind101.com/re%20re/79/the second place of the id part does what i want, but the second one does not work, and the "$subject=str_ireplace("%20","-",$subject);" seems just suit for one spaces between two words, how about if i got more than 2 words? it will become like "http://businessfind101.com/re--------------re/79/.but i just want the result like "http://businessfind101.com/re-re/79/", it does not matter how many spaces between them. Link to comment Share on other sites More sharing options...
slipszenko Posted August 27, 2007 Share Posted August 27, 2007 Ok. Not too sure about that atm. If it's live can you link me to it? otherwise i'd suggest replacing str_ireplace with str_replace and see if that has any effect. Link to comment Share on other sites More sharing options...
slipszenko Posted August 27, 2007 Share Posted August 27, 2007 Fixed It Now, in the string the space is stored as " " not "%20". Don't know why i didnt think of this earlier! Sorry! So here is the working version (as far as i know): <?phpif(isset($_POST['subject'])){$subject=$_POST['subject'];$id=$_POST['id'];}$subject = trim($subject);$subject = str_replace(" ","-",$subject);$id = trim($id);$id = str_replace(" ","",$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> Link to comment Share on other sites More sharing options...
xbl1 Posted August 28, 2007 Author Share Posted August 28, 2007 Fixed It Now, in the string the space is stored as " " not "%20". Don't know why i didnt think of this earlier! Sorry! So here is the working version (as far as i know):<?phpif(isset($_POST['subject'])){$subject=$_POST['subject'];$id=$_POST['id'];}$subject = trim($subject);$subject = str_replace(" ","-",$subject);$id = trim($id);$id = str_replace(" ","",$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> Hi Slipszenko;thanks for your time, i will try it tomorrow, i am tired for my work today. Link to comment Share on other sites More sharing options...
xbl1 Posted August 29, 2007 Author Share Posted August 29, 2007 Fixed It Now, in the string the space is stored as " " not "%20". Don't know why i didnt think of this earlier! Sorry! So here is the working version (as far as i know):<?phpif(isset($_POST['subject'])){$subject=$_POST['subject'];$id=$_POST['id'];}$subject = trim($subject);$subject = str_replace(" ","-",$subject);$id = trim($id);$id = str_replace(" ","",$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> It still give me the result with many "-------" of the sign.http://www.mysite.com/jhh-------jj---------j/87not like the following;http://www.mysite.com/jhh-jj-j/87 Link to comment Share on other sites More sharing options...
xbl1 Posted August 29, 2007 Author Share Posted August 29, 2007 i think i may go to try regular expression. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 29, 2007 Share Posted August 29, 2007 You could use a regular expression, or you could just write your own loop that will remove more then one dash in a row. $new_url = "";$found_dash = false;for ($i = 0; $i < strlen($url); $i++){ if ($url[$i] == "-") { if ($found_dash) continue; else $found_dash = true; } else $found_dash = false; $new_url .= $url[$i];} After that, $new_url will contain the URL with excess dashes removed. Link to comment Share on other sites More sharing options...
jesh Posted August 29, 2007 Share Posted August 29, 2007 The regular expression to match multiple hyphens is pretty trivial: /[-]+/gIn javascript, you could do it like so: url = url.replace(/[-]+/g, "-"); Someone here could convert that to PHP, I'm sure. Link to comment Share on other sites More sharing options...
jhecht Posted August 30, 2007 Share Posted August 30, 2007 The URL you gave as an example isn't a PHP defined thing. Its an apache thing defined using Mod_Rewrite. <IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^[a-zA-Z0-9\-]+/[0-9]+/$ yourPage.php?firstVar=$1&second=$2</IfModule> IfModule code borrowed from WordpressYou would then have to store something in database with the slash-converted variable of the title name or whatever it is you are using. <?php$string = "hello you";//There are 2 spaces next to each other;$string = preg_replace("/(.*)\s+(.*)/i","\\1-\\2",$string);//That should do it;?> Link to comment Share on other sites More sharing options...
xbl1 Posted August 30, 2007 Author Share Posted August 30, 2007 You could use a regular expression, or you could just write your own loop that will remove more then one dash in a row.$new_url = "";$found_dash = false;for ($i = 0; $i < strlen($url); $i++){ if ($url[$i] == "-") { if ($found_dash) continue; else $found_dash = true; } else $found_dash = false; $new_url .= $url[$i];} After that, $new_url will contain the URL with excess dashes removed. Thanks justsomeguy;That's what i want.Thanks for your time again. Link to comment Share on other sites More sharing options...
xbl1 Posted August 30, 2007 Author Share Posted August 30, 2007 HI Jesh & Jhecht;i am going to take a couple day to read and understand some materials about regular expression, then i will come back your solution, i have forget them already. Link to comment Share on other sites More sharing options...
xbl1 Posted September 1, 2007 Author Share Posted September 1, 2007 The regular expression to match multiple hyphens is pretty trivial: /[-]+/gIn javascript, you could do it like so:url = url.replace(/[-]+/g, "-"); Someone here could convert that to PHP, I'm sure. Thanks Jesh;Your one will get the final result as http://www.mysite.com/_how_to_solve_the_problem_.if some string as following; $$ 999 how to **** 00 solve the problem ?????.the final result should like thishttp://www.mysite.com/how_to_solve_the_problemNow i have solved the problem already;$subject=strtolower($subject);$subject=preg_replace('%^[^a-z]+%', '', $subject);$subject=preg_replace('%[^a-z]+$%', '', $subject);$subject=trim($subject);$subject=preg_replace('%[^a-z]+%', '_', $subject); $id=trim($id);Anyway, thanks a lot. Link to comment Share on other sites More sharing options...
xbl1 Posted September 1, 2007 Author Share Posted September 1, 2007 The URL you gave as an example isn't a PHP defined thing. Its an apache thing defined using Mod_Rewrite.<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^[a-zA-Z0-9\-]+/[0-9]+/$ yourPage.php?firstVar=$1&second=$2</IfModule> IfModule code borrowed from WordpressYou would then have to store something in database with the slash-converted variable of the title name or whatever it is you are using. <?php$string = "hello you";//There are 2 spaces next to each other;$string = preg_replace("/(.*)\s+(.*)/i","\\1-\\2",$string);//That should do it;?> Hi;your one does not do the work properly.if the string like " && test ((( me ???".you will have the result same as the original of the string.But anyway, i have known how to do already. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.