Jump to content
Sign in to follow this  
westman

preg_replace for 20% in url

Recommended Posts

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?

Share this post


Link to post
Share on other sites

so...

 

$new_url = preg_replace(''/[20%]+/', '" "', $new_url);

or

$new_url = preg_replace(''/[20%]+/', ' ', $new_url);

or

$new_url = preg_replace(''/[20%]+/', " ", $new_url);

?

Share this post


Link to post
Share on other sites

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 "%%%"

  • Like 1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

 

 

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.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...