php urlencode encodes wrong? - php

If I try to encode the url
http://herthabsc.de/index.php?id=3631&tx_ttnews[tt_news]=13144&cHash=9ef2e9ee006fb16188ebf764232a0ba9
with urlencode() or http_build_query() it gives me the result
http%3A%2F%2Fherthabsc.de%2Findex.php%3Fid%3D3631%26%23038%3Btx_ttnews%5Btt_news%5D%3D13144%26%23038%3BcHash%3D9ef2e9ee006fb16188ebf764232a0ba9
But that's not what it should be. Is there a known bug? Or problems in use with wordpress?

You've double encoded the URL. Running urldecode() on your output string is giving me the following: http://herthabsc.de/index.php?id=3631&tx_ttnews[tt_news]=13144&cHash=9ef2e9ee006fb16188ebf764232a0ba9
EDIT:
try the following
urlencode(html_entity_decode('http://herthabsc.de/index.php?id=3631&tx_ttnews[tt_news]=13144&cHash=9ef2e9ee006fb16188ebf764232a0ba9'));

Related

PHP file_get_contents() Chinese character ERROR CODE

I use file_get_contents() to download a JSON. There're some Chinese characters in the URL, I tried to print the URL out, it's OK. But when I ran the program, the URL I put in the function became error code. How do I know that is this URL links to a JSON that links to a MySQL request, and in the console of MySQL, I saw the URL became error code. I tried lots of ways to change URL string to UTF-8 or GB2312, etc, but none of that works. I Wish I could get help here, thanks.
Its very difficult to understand your question. I think i understood the first part of your question:
I use file_get_contents() to download a JSON. There're some Chinese
characters in the URL, I tried to print the URL out, it's OK. But when
I ran the program, the URL I put in the function became error code.
You try to access a URL containing chinese characters using file_get_contents():
The answer to this is:
You need to encode the part of the url containing chinese characters using urlencode() or rawurlencode().
The main difference between urlencode()and rawurlencode() is, that urlencode() converts spaces to +. rawurlencode() converts spaces to %20.
urlencode is used for Query Parameters as example ?q=my+search+key, in every other case you use rawurlencode.
Example:
$test = 'http://www.example.com/'.rawurlencode('以怎么下载').'.html';
print_r($test);
// $html = file_get_contents($test);
// output:
http://www.example.com/%E4%BB%A5%E6%80%8E%E4%B9%88%E4%B8%8B%E8%BD%BD.html
I hope it solves your problem.

How to send string(say: public_Key) having special characters in URL?

When I said simple string It works fine, but when I send my public_key string in the same way, It shows error.
I have tried the urlencode() method.
<iframe src="http://local.abc.com/formulaList?id=<?php echo $public_key; ?>" >
base64() doesn't work because its output can contain slashes (and anyway, most public key strings are already base64 encoded)
htmlspecialchars() escapes HTML special chars as its name implies which has nothing to do with urls (for example, é will be converted to é)
urlencode() is the right function to use but keep in mind an URL shouldn't be too long as explained in this SO answer
What problem did you encounter when using urlencode() ?
I write something this might work.
$url = htmlspecialchars("wdsd#ccddsd*");
header("Location: https://www.google.com?key={$url}");
this will work for you!

Removing %2520 and other nonstandard characters from URL in obj c

I am getting a URL from server and trying to load the URL in webview. The issue is that the url which I am getting contains non standard characters. The URL is:
https//p-r3.test.abc.com:443%2Ftablet%2Fjsp%2Fgift%2Fipad%2Fgifter%2FgitGiftList.jsp%3FregId%3D74500002%26filterBy%3DviewAll%26pageId%3DourGifty%26sort%3Dcategory%26groupBy%3Dcategory%26view%3Dlist%26categoryId%3D%26addCat%3Dcat100540004&title=re%20-&imgurl=https%3A%2F%2Fm-r3-testy.tr.com%3A443%2Ftablet%2Fimages%2Ft_Full.jpg%3Fwid%3D300%26hei%3D300.
I need to remove characters like %2520, %2F, %3D and other non standard characters from the URL. Anyone has idea to remove this encoding.
Any help would be appreciated
Thanks
%2520 is simply a double-encoded space. Encode it once and you get %20, encode it twice and you get %2520. It's not "non-standard", it's just poorly coded. In theory, there's no reason why you can't just replace %2520 with a space, but for all I know the server-side code is expecting the double-encoded string.
Found the answer.I am removing the encoding using the built in function of iOS.abc = [def stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and i am loading abc in webview.It is working fine.
Thanks all for the responses.
You seem to have an urlencode() too many, or an urldecode() too few, in the code processing the URL server side.
To avoid multiple encoding, Remove any encoding first
_pdfUrl = [ _pdfUrl stringByRemovingPercentEncoding];
_pdfUrl = [_pdfUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

PHP get url with special characters without urlencode:ing them!

I would like file_get_contents to get a url that looks like this: http://wapedia.mobi/sv/Gröt
The problem is that it requests (can't post entire link, sorry): ...wapedia.mobi/sv/Gr%C3%B6t which you can see has been urlencoded, that page does not give me any results.
How can I do this?
According to the PHP manual, you must specifically encode a URL if it contains special characters. This means the function itself should do no special encoding. Most likely your URL is being encoded before being passed to the function, so pass it through urldecode first and see what happens.
Edit: You're saying the encoding is being messed up. Again the PHP manual specifically states that you need to encode urls prior to passing them to file_get_contents. Try encoding the URL, then passing it to the function.
$url = urlencode($url);
file_get_contents($url);

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