title: PHP URL encoding retaining invalid url characters
body: <p>I need to replace url invalid characters with something url valid then convert it back again.</p>
  
  <p>This is for a search page with a url like <a href="http://my.site/search/this-is-a-search" rel="nofollow noreferrer">http://my.site/search/this-is-a-search</a>, the search form POSTS then the user redirected to the new url.</p>
  
  <p>Php has functions urlencode and urldecode however these do not work at all, and leave invalid characters in my url.</p>
  
  <p>Surely I don't need to reinvent the wheel here.</p>