To convert an URL to the "first format", you can use the PHP function urlencode
.
Now, for the "why", the answer can probably be found in the RFC 1738 - Uniform Resource Locators (URL).
Quoting some paragraphs :
Octets must be encoded if they have no corresponding graphic
character within the US-ASCII coded character set, if the use of the
corresponding character is unsafe, or if the corresponding character
is reserved for some other interpretation within the particular URL
scheme.
No corresponding graphic US-ASCII:
URLs are written only with the graphic printable characters of the
US-ASCII coded character set. The octets 80-FF hexadecimal are not
used in US-ASCII, and the octets 00-1F and 7F hexadecimal represent
control characters; these must be encoded.
A space has the code %20 -- it's not in the range 00-1F, so it should be encoded for that reason... But, a bit later :
Unsafe:
Characters can be unsafe for a number of reasons. The space
character is unsafe because significant spaces may disappear and
insignificant spaces may be introduced when URLs are transcribed or
typeset or subjected to the treatment of word-processing programs.
And here, you know why the space character has to be escaped/encoded too ;-)