Urlencode everything but slashes?

You can do like this:

$url = "http://www.google.com/myprofile/id/1001";
$encoded_url = urlencode($url);
$after_encoded_url = str_replace("%2F", "/", $url);

  1. Split by /
  2. urlencode() each part
  3. Join with /

Basically what @clovecooks said, but split() is deprecated as of 5.3:

$path = '/path with some/illegal/characters.html';

$parsedPath = implode('/', array_map(function ($v) {
    return rawurlencode($v);
}, explode('/', $path)));

// $parsedPath == '/path%20with%20some/illegal/characters.html';

Also might want to decode before encoding, in case the string is already encoded.

Tags:

Php

Urlencode