4

I am getting a URL that contains amp;. Is there any way to remove this as currently I tried URLDecode function, but It's not working. Do I need to remove It using simple string replacement or Is there any better way to do this?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jyotish Singh
  • 2,065
  • 3
  • 20
  • 28
  • Can you perhaps show the URL *(just with the hostname changed)*? – user692942 May 18 '16 at 13:18
  • https://example.com?xyz=1&ID=6184&KEY=6e8a64fc-1c1e-11e6-9059-b97210fab7z6 – Jyotish Singh May 18 '16 at 13:20
  • 2
    You shouldn't have `&` in there in the first place, they are HTML encoded entities which is why `URLDecode()` won't work. If it was URL encoded it would be `%3D` not `&`. You want to look at why the URL is coming through like that I guess somewhere someone has used `HTMLEncode()` in error. – user692942 May 18 '16 at 13:22
  • Well thanks however I can't ask my client for any change so I have to manage at my own end therefor Is there any better way to solve this? – Jyotish Singh May 18 '16 at 13:26
  • If it's HTML encoded then you need to decode it - http://stackoverflow.com/a/6116611/692942 or if it's just the `&` that is encoded use something like `url = Replace(url, "amp;", "")`, but you when have to `Response.Redirect()` *(assuming your using ASP)* to re-run the page with the correct parameters. The issue is the clients it should be them making sure they pass you a correctly formed URL. – user692942 May 18 '16 at 13:28
  • Can you just replace those out: `url=Replace(url, "amp;", "")` – JNevill May 18 '16 at 13:31
  • @JNevill It depends if the client sent the URL incorrect it's already to late, unless you parse the URL *(using `Replace()` or whatever)* and re-run the request *(be it `Response.Redirect()` or `ServerHTTPRequest.Send()`)*. – user692942 May 18 '16 at 13:39
  • Sorry @Lankymart. I didn't see your suggestion `replace()` right before my comment. :/ Not paying attention this morning. – JNevill May 18 '16 at 13:40
  • @JNevill I was mid edit when your comment came through, no apology needed. – user692942 May 18 '16 at 13:41

2 Answers2

5

As @Lankymart pointed out URLDecode only works on URL-encoded characters (%26), not on HTML entities (&). Use a regular string replacement to change the HTML entity & into a literal ampersand:

url = Replace(url, "&", "&")
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • What will that do though if the URL has already been received from the client? At some point the OP will have to "re-run" the request after the URL has been stripped of `&` I guess. – user692942 May 18 '16 at 14:05
  • I don't think we have enough information to even speculate about where the URL came from or what he's going to do with it, so I just answered the immediate question (or rather made the comments into an actual answer). – Ansgar Wiechers May 18 '16 at 14:08
  • See your point, but OP did [kind of confirm](http://stackoverflow.com/questions/37300806/how-to-remove-amp-from-url/37301978?noredirect=1#comment62124271_37300806) they are not in control of the URL they receive. – user692942 May 18 '16 at 14:10
  • 1
    Still, without him showing (some of) his code and/or explaining what he's trying to accomplish we'd merely be speculating, and I prefer not to do that. As soon as there's more information everyone is free to post a better answer (or improve this one, since I posted it as a wiki answer). – Ansgar Wiechers May 18 '16 at 14:14
0

In Angular I added amp; to the params names

    this.activatedRoute.queryParams.subscribe(params => {
  this.user_id = params['user_id'];
  this.practice_id = params['amp;practice_id'];
  this.patient_id = params['amp;patient_id'];
});