Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Urlencode everything but slashes?

Tags:

php

urlencode

Is there any clean and easy way to urlencode() an arbitrary string but leave slashes (/) alone?

like image 410
Cobra_Fast Avatar asked Feb 13 '14 13:02

Cobra_Fast


People also ask

Is Urlencode necessary?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

What is the point of Urlencode?

Encodes text and merge field values for use in URLs by replacing characters that are illegal in URLs, such as blank spaces, with the code that represent those characters as defined in RFC 3986, Uniform Resource Identifier (URI): Generic Syntax.

What is meant by Urlencode and Urldecode?

urlencode () is the function that can be used conveniently to encode a string before using in a query part of a URL. This is a convenient way for passing variables to the next page. urldecode() is the function that is used to decode the encoded string.


2 Answers

  1. Split by /
  2. urlencode() each part
  3. Join with /
like image 80
Idan Arye Avatar answered Sep 22 '22 00:09

Idan Arye


You can do like this:

$url = "http://www.google.com/myprofile/id/1001";
$encoded_url = urlencode($url);
$after_encoded_url = str_replace("%2F", "/", $url);
like image 34
Ashwin Parmar Avatar answered Sep 20 '22 00:09

Ashwin Parmar