westman 10 Posted December 29, 2014 Report Share Posted December 29, 2014 hi all i have been trying for 7 hours now and i can not find the best way to change 20% from a url string to a space. I am trying to change this "20%" to this " ". this is what i have.... $new_url = preg_replace('"20%"', '" "', $new_url); any help? Quote Link to post Share on other sites
Don E 125 Posted December 29, 2014 Report Share Posted December 29, 2014 try '/[20%]+/' Quote Link to post Share on other sites
westman 10 Posted December 29, 2014 Author Report Share Posted December 29, 2014 so... $new_url = preg_replace(''/[20%]+/', '" "', $new_url); or $new_url = preg_replace(''/[20%]+/', ' ', $new_url); or $new_url = preg_replace(''/[20%]+/', " ", $new_url); ? Quote Link to post Share on other sites
Don E 125 Posted December 29, 2014 Report Share Posted December 29, 2014 Try: $new_url = preg_replace('/[20%]+/', ' ', $new_url); Quote Link to post Share on other sites
Ingolme 1,019 Posted December 29, 2014 Report Share Posted December 29, 2014 Are you sure you don't mean %20? That's the escape code for the space character in URLs. There's no need for a regular expression here. Use str_replace $new_url = str_replace('%20', ' ', $new_url); Try: $new_url = preg_replace('/[20%]+/', ' ', $new_url); This would cause the program to replace any sequence of any length that contains 2, 0, and % with a space. It would match things like "2%02" and "%%%" 1 Quote Link to post Share on other sites
westman 10 Posted December 29, 2014 Author Report Share Posted December 29, 2014 foxy, again, thank you so much. lol 20% and %20 Quote Link to post Share on other sites
Don E 125 Posted December 29, 2014 Report Share Posted December 29, 2014 This would cause the program to replace any sequence of any length that contains 2, 0, and % with a space. It would match things like "2%02" and "%%%" Yes. I was thinking since a URL can contain many %20, that would be ideal (to add the character class [] ) but upon further observation it would also remove the 20 from a URL like mypage20.php for example and that's not good. So for the sake of understanding, would have this been the correct way?: $new_url = preg_replace('/%20/', ' ', $new_url); Thanks Quote Link to post Share on other sites
Ingolme 1,019 Posted December 30, 2014 Report Share Posted December 30, 2014 Yes. I was thinking since a URL can contain many %20, that would be ideal (to add the character class [] ) but upon further observation it would also remove the 20 from a URL like mypage20.php for example and that's not good. So for the sake of understanding, would have this been the correct way?: $new_url = preg_replace('/%20/', ' ', $new_url); Thanks Yes, that would be correct. Since there are no real rules a regular expression isn't needed, you're just trying to match a literal string here. 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.