replace PHP Escape sequence characters from string - php

I have string of URL with encoded google poly line. It has some time characters which are as same as PHP Escape Sequence (http://php.net/manual/en/regexp.reference.escape.php) such as \v and other too. When I call this URL to download image from remote server the URL string get's altered and does not work.
How to handle Escape Sequence to let php script download the image properly.
Below URL in DB:
http://maps.googleapis.com/maps/api/staticmap?size=275x270&sensor=true&markers=-31.95099,115.86053&markers=-31.952055,115.865051&path=weight:4%7Ccolor:0x0000FF%7Cenc:tl_bEi_dbUVol#gTug#aG_j#qe#_Le]_ZtO_i#tLsm#xd#sPdTu\sZeOq_#nZy_#wDsTah#_Dyl#cEg_#uImf#mKqU{WgJaEwd#mIs_#y[k]k^g[qi#GaUy\_Eqj#qi#aB{g#uJwi#uBe_#sYk^c_#ef#uOci#|A_h#_M}h#}Hcg#{Oi`#k]kIeh#tQsg#lKwa#fKs]~Fse#vi##v`#oIti#Dbc#fKza#aIrEe`#dh#hBvi#Q`[mOb^{Mpa#eYpP_k#Eah#e#an#xFwl#jBmn#Zaq#bAmp#nFap#gGon#_Iio#m#{p#nCgp#eE_p#~Hko#hBaq#jB}p#lYyXpe#mJv`#i\nc#}Wpg#_Obi#Dve#`S~d#bR~F~i#dVdd#|WpShJic#db#nKzGvo#ne#`Et^rXyE`o#mRhi#iCvp#oZrd#eJvo#lVdg#hKrn#cFzn#v[pMfh#\xCfYiX|Sf#~n#eNld#}V`]qNjZs`#hYwa#rQeXrUcNxf#}Ipn#lApp#rUzf#|h#rAvi#Ydi#mBvf#tQvb#rWbHzo#~Gjo#r]tb#vWlg#rXzd#eBbj#eTvi#uSzj#{Fdp#uJ~m#k\nc#i]fb#c]jb#o]~a#c^ra#ySfd#yQve#gZrUu\lLc`#x]aItm#fAxi#nHtl#pRjh#
Below URL out put from PHP:
http://maps.googleapis.com/maps/api/staticmap?size=275x270&sensor=true&markers=-31.95099,115.86053&markers=-31.952055,115.865051&path=weight:4%7Ccolor:0x0000FF%7Cenc:tl_bEi_dbUVol#gTug#aG_j#qe#_Le]_ZtO_i#tLsm#xd#sPdTu\sZeOq_#nZy_#wDsTah#_Dyl#cEg_#uImf#mKqU{WgJaEwd#mIs_#y[k]k^g[qi#GaUy\_Eqj#qi#aB{g#uJwi#uBe_#sYk^c_#ef#uOci#|A_h#_M}h#}Hcg#{Oi`#k]kIeh#tQsg#lKwa#fKs]~Fse#vi##v`#oIti#Dbc#fKza#aIrEe`#dh#hBvi#Q`[mOb^{Mpa#eYpP_k#Eah#e#an#xFwl#jBmn#Zaq#bAmp#nFap#gGon#_Iio#m#{p#nCgp#eE_p#~Hko#hBaq#jB}p#lYyXpe#mJv`#ic#}Wpg#_Obi#Dve#`S~d#bR~F~i#dVdd#|WpShJic#db#nKzGvo#ne#`Et^rXyE`o#mRhi#iCvp#oZrd#eJvo#lVdg#hKrn#cFzn#v[pMfh#%C3%8FYiX|Sf#~n#eNld#}V`]qNjZs`#hYwa#rQeXrUcNxf#}Ipn#lApp#rUzf#|h#rAvi#Ydi#mBvf#tQvb#rWbHzo#~Gjo#r]tb#vWlg#rXzd#eBbj#eTvi#uSzj#{Fdp#uJ~m#kc#i]fb#c]jb#o]~a#c^ra#ySfd#yQve#gZrUu\lLc`#x]aItm#fAxi#nHtl#pRjh#
The URL from DB show path properly, where as same url when out put from PHP does not show path at all. Also the length of url gets reduced when output from PHP.
thanks
Waqas

You could use good old base64encode to encode the url and before you store in the database, if you have access to that code. Uses 33% more space, but helps with escape headaches. PHP - Base64 Encode

Related

How do I sent an encrypted hash over URL in CakePHP 3?

I'm working on a reset password function that matches hashes to allow for the user to reset. The only trouble is the hash that was created cannot be passed over URL without breaking the page. For example, my hash is this:
http://localhost/users/changeResetPassword/e0b4ab1d2cdc5742c7b5f72ef6c2935dadfe458dc275b7419d9f1ac66461aa20%0F5%3A%C6%5C%26%2A%E4%D5%ACA%94%ADV%BF%EB%CAz%97O%1F%7D%F0h~%E3-.%FF%B4z%5E%1AQ%B8%8Ca%BC500%2A%EC%7B%FA%AF8%E3%2A%7F%BA%A4y%03%AE%29%94%09%26%9E%29e%E5%DEn%1At%C1%EC%F7%D4x%EAvlA%BE%5B%0D%CF
All of these % seem to break the page, because I get this error:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If I take away everything up to the last % and try it again, the page loads fine. It's just some characters in the URL seem to bug everything out:
http://localhost/users/changeResetPassword/e0b4ab1d2cdc5742c7b5f72ef6c2935dadfe458dc275b7419d9f1ac66461aa20
This loads fine, so I know it's just a URL problem. Any ideas?
Base64 encode the hash and pass it along, then decode when received. Cake URI parsing likely breaks because it'll try to interpret those %xx as html encoded values but it does not appear that is what they are since you have stuff like %03 and from the link: "The ASCII device control characters %00-%1f were originally designed to control hardware devices. Control characters have nothing to do inside a URL"
With regards to ndm's comment indicating you may additionally need to URL encode the base64 string since base64 can contain the characters + = / I'd recommend you also look at url encoded forward slash is breaking url to see why simply encoding the problematic characters might also be problematic. If the current solution of passing unencoded base64 strings is not causing any issues with your rewrite rules I would recommend you keep it as it.

Angled brackets being escaped from url in PHP

I am trying to call the USPS API that takes in the zip code and returns XML containing the City Name of the given zip code.
Here is the URL they require:
http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup
&XML=<CityStateLookupRequest USERID="xxxxxxx"><ZipCode ID= "0">
<Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>
In my PHP file, when I echo out the above URL, this is what I get:
http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=90007
All the XML part of the URL is missing. I need to get curl data from the URL.
Anyone know what I could be missing?
Anyone know what I could be missing?
Probably. Maybe, yes. What you describe in your posting sounds like an encoding problem. So you are missing the right encoding.
As you are talking about an URL that is likely URL encoding. Some characters - like space - have a special meaning inside an URL so you can not just use any character as you like, but you need to encode all characters properly.
The exact description how you need to formulate an URL incl. the exact description how URL encoding works is outlined in 2. Characters in the internet standard RFC3986.
PHP functions related to URL encoding are urlencode() and rawurlencode and more likely useful in your case http_build_query().
http://php.net/manual/en/function.urlencode.php should get you started if you want to encode the xml into the URL.

php function to decode arabic text in url to percent encoding like firefox

i have this url
http://al-mashhad.com/News/النيابة-تستمع-لأقوال-خالد-يوسف-في-بلاغه-ضد-أبو-إسم/141274.aspx
firefox change it to
http://al-mashhad.com/News/%D8%A7%D9%84%D9%86%D9%8A%D8%A7%D8%A8%D8%A9-%D8%AA%D8%B3%D8%AA%D9%85%D8%B9-%D9%84%D8%A3%D9%82%D9%88%D8%A7%D9%84-%D8%AE%D8%A7%D9%84%D8%AF-%D9%8A%D9%88%D8%B3%D9%81-%D9%81%D9%8A-%D8%A8%D9%84%D8%A7%D8%BA%D9%87-%D8%B6%D8%AF-%D8%A3%D8%A8%D9%88-%D8%A5%D8%B3%D9%85/141274.aspx
i want php function to do that task
i tryied urencode(), but its do that
http%3A%2F%2Fal-mashhad.com%2FNews%2F%D8%A7%D9%84%D9%86%D9%8A%D8%A7%D8%A8%D8%A9-%D8%AA%D8%B3%D8%AA%D9%85%D8%B9-%D9%84%D8%A3%D9%82%D9%88%D8%A7%D9%84-%D8%AE%D8%A7%D9%84%D8%AF-%D9%8A%D9%88%D8%B3%D9%81-%D9%81%D9%8A-%D8%A8%D9%84%D8%A7%D8%BA%D9%87-%D8%B6%D8%AF-%D8%A3%D8%A8%D9%88-%D8%A5%D8%B3%D9%85%2F141274.aspxstring(296) "http://al-mashhad.com%2FNews%2F%D8%A7%D9%84%D9%86%D9%8A%D8%A7%D8%A8%D8%A9-%D8%AA%D8%B3%D8%AA%D9%85%D8%B9-%D9%84%D8%A3%D9%82%D9%88%D8%A7%D9%84-%D8%AE%D8%A7%D9%84%D8%AF-%D9%8A%D9%88%D8%B3%D9_-%D9_%D9%8A-%D8%A8%D9%84%D8%A7%D8%BA%D9%87-%D8%B6%D8%AF-%D8%A3%D8%A8%D9%88-%D8%A5%D8%B3%D9%85%2F141274.aspx"
and i want it change the arabic text only, just like mozilla firefox
because its corrupt http:// and /
You could split the full URI just taking into account the slash character (/) and encode only the final fragment.
You could also access the server information. In PHP you can access the variable $_SERVER['SCRIPT_NAME'] to recover the part of the URI that you want to encode. I do not know about how that is made in ASP.

special characters in url filename cause problems

I have the following www.mywebsite.com/upload/server/php/files/foto/test/Aston_Martin_DBS_V12_coupé_(rear)_b-w.jpg
This file is uploaded trough a script. The file exists on the server.
However, because the special character in the url (é), I am experiencing some problems.
The filename on the server is Aston_Martin_DBS_V12_coup%C3%A9_(rear)_b-w.jpg, which is correct. However somehow my browser (Chrome) requests this page as ISO-8859-1 instead of UTF-8.
Therefore, I get a 404.
I am using jQuery file upload plugin.
I deleted my answer from here and i wrote new:
Usually websites does not contain files with non-standard characters. Files usually have removed non standard characters, sometimes that characters are replaced by similar standard chars (Polish ą to a, ś to s). For example - im renaming files manually, or when i have a lot of files - i just use bash or php script that removes/replaces that characters in filenames on server.
Anyway, if you HAVE TO use original filenames - you have to decode them from ISO and encode them to UTF8.
Take look at that php code fragment here:
how to serve HTTP files with special characters
Some special Charater make problem in url for filename
like
+ ,#,%,&
For those file which are accessing through url make file which not contain above letters
forex
str_replace(array(" ","&","'","+","#","%"),"-","filename")
it will works fine
If the filename contains the % character codes, you will need to encode those in your URL. Try accessing Aston_Martin_DBS_V12_coup%25C3%25E9_(rear)_b-w.jpg

Is it possible to output the '&' sign in an xml file?

I have a php which generates an xml file and prints it on screen.
Amongst other variables, it prints an Image link.
The problem is that if this Image link has an '&' character in it, I get an xml error because it isn't encoded properly.
So I solve it by replacing the & sign with &.
Atleast I thought it was solved, now the link to the image is for example like this:
www.domain.com/phones & equipment/img1.jpg
which causes a 404 file not found.
The real path is
www.domain.com/phones & equipment/img1.jpg
So how can I solve this then?
I would prefer not to change the folder names, I simply didn't know this when I created the folders.
Thanks
If it's a URL, you might want to URL encode it instead:
www.domain.com/phones%20%26%20equipment/img1.jpg
try to
www.domain.com/phones+%26+equipment/img1.jpg
You should url encode the link using php urlencode() function. The code for '&' is "%26".
Additionally, if you check IANA RFC regarding URL/URI, you will see that space character is not a valid character and shouldn't be present inside REQUEST URI. Having your URL like www.domain.com/phones-and-equipment/img1.jpg would be much beneficial from SEO standpoint as well.
ADDENDUM: For example, check page 2, section "Unsafe" of RFC 1738 and see why non-printable and non-US-ASCII characters are not safe.
use html_entity_decode to convert & back into an &
see http://php.net/manual/en/function.html-entity-decode.php
The correct representation of the ampersand in an XML file is &. If that isn't working, the problem is with the code that is reading the XML file and deferencing the URI.

Categories