Removing the "%" encoding in URL string - php

I am attempting to open a page with window.open and it's not working. The path shown is like xyz/a%20b%20c%20.pdf, but it is supposed to be xyz/abc.pdf. If I remove the % and 20 manually, it works, how can I remove these characters using PHP?

Use urldecode:
(PHP 4, PHP 5)
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
Example
echo urldecode('xyz/a%20b%20c%20.pdf');

This is known as URL Encoding. You need to decode the string. If you are using jQuery you should check out the URL Encode plug in.

You need to urldecode (as stated above).
However, you say that you can remove the %20 and it will work. I would say you need them, they decode to spaces. Check it out using this online url decoder:
http://www.convertstring.com/EncodeDecode/UrlDecode
it decodes to:
xyz/a b c .pdf
not
xyz/abc.pdf

Related

Unable to get a value from url

Unable to get a value from url.
localhost/ddd.php?udh=%05%00%03%6d%03%01
When I try to copy paste this url to address bar "localhost/ddd.php?udh=%05%00%03%6d%03%01" it converts to "http://localhost/ddd.php?udh=%05%00%03m%03%01"
Is there any explanation?
Also I am not able to get $_GET['udh'], it prints:
array (size=1)
'udh' => string '�m' (length=6)
From W3C Schools:
URL Encoding (Percent Encoding)
URLs can only be sent over the Internet using the ASCII character-set.
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.
So when you put the string %05%00%03%6d%03%01 in the browser it automatically tries do decode it searching for %character, in your case it happens that %6d = m.
Not sure if you are using % as a separator but you could change to : or | and use explode to extract it (http://php.net/manual/en/function.explode.php)
May be you should encode your message using urlencode() and when accessing the message using $_GET you have to decode it using urldecode().
HTML
<a href ="localhost/ddd.php?udh=".<?php urlencode('message');?>></a>
PHP
$value = urldecode(isset($_GET['udh']));

PHP converting URL string to ASCII characters

I have a string of Characters that is passed in a URL.
The string happens to contain a group of characters that is equivalent to an ASCII code.
When I try to use the string on the page using the $_GET command, it converts the part of the string that is equivalent to the ASCII code to the ASCII code instead of passing the actual string.
For example the URL contains a string Name='%bert%'. But when I echo out $_GET['Name'] I get '3/4rt%' instead of '%bert%'. How can I get the actual text?
You're not escaping your data properly.
If you want to use %bert% in a URL, you need to encode your % as %25, making your query string value %25bert%25.
% in a URL means that the next two characters are going to be some encoded entity, so if you want to use it literally, it must be encoded this way.
You can read more information here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
try passing Name='%25bert%25' instead of Name='%bert%'.
Note: %25 acts as escape character for % is url query string!

Dynamic URL in PHP not working when passing "&" in href tag

I have the following URL
http://localhost:8777/business.php?id=Mobiles and Tablets
When I am passing "and" in id it is redirecting me to the desired page but when I am passing
http://localhost:8777/business.php?id=Mobiles & Tablets
& i.e. ampersand in id it is giving 404 error.
My PHP Code is like this:
<? echo $cat;?>
You have to use urlencode() in your php code.
<? echo $cat;?>
It will generate link like:
http://localhost:8777/business.php?id=Mobiles+%26+Tablets
Space will be converted to +, and & will be converted to %26
use php urlencode()
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
And use urldecode() to decode your encoded argument
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.

get values in url

I need to get values in the url as it is
ex:
http://www.example.com/index?url=1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA==
but vriable url give me value of "1 LY2ePh1pjX4tjZ4 GS393Y2pjd16Cbq63T3tbfzMzd16CarA=="
Even though i have expected "1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA=="
any one can help me for this or know the reason
You see, you need to encode certain characters if you need to send them in a URL. For further references, I suggest you should read this Page. It seems that the URL you are getting isn't being encoded properly. If the URL is coming from your site, then I would suggest you to encode it properly.
In PHP, there is a function called urlencode, which may help you with this task.
A short explanation
URLs can only be sent over internet using ASCII character set.If you want to send characters which is outside this set, you need to encode it.URL encoding replaces unsafe ASCII characters with % followed by two hexadecimal digits corresponding to the character values in the ISO-8859-1 character-set.
The client sending the request apparently isn't URL encoding the value correctly. You can re-encode it after it's being decoded like this:
urlencode($_GET["url"])
IT convert %2B to space
The parameter you sent is wrong, it should have been encoded like so..
<?php
echo '<a href="http://www.example.com/index?url=', urlencode('1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA=='), '">';
?>
i have added encoding correctly now,It convert == correctly, but + sign encode to %2B correctly but in decode process it convert to space
As it seems that you’re having a Base-64 value there: You can use the URL safe alphabet for Base-64 that uses - and _ instead of + and / respectively:
$base64 = "1+LY2ePh1pjX4tjZ4+GS393Y2pjd16Cbq63T3tbfzMzd16CarA==";
// plain Base-64 to URL safe Base-64
$base64_safe = strtr($base64, '+/', '-_');
// URL safe Base-64 to plain Base-64
$base64 = strtr($base64_safe, '-_', '+/');
And if you know the length of the data, you can also omit the = padding:
rtrim($base64, '=')

PHP URL encoding method

How can I have PHP display this URL correctly?
Is there a working encoding method I can use that converts all of
253A%252F%252F
to
://
in
https%253A%252F%252Fvideos-private.s3.amazonaws.com%252Flesson05.flv
?
You should use double urldecode, i.e.,
$beuty_url = urldecode(urldecode("https%253A%252F%252Fbassrxprivate.s3.amazonaws.com%252Flesson05.flv"));
echo $beuty_url;
urldecode ;)
The urldecode function decodes any %## encoding in the given string.
In your "output" string you have %253A and so on.
Remove those %25 and everything will be fine.

Categories