2

I'm using PHP to encode a Url and after that I'm using the Url in Javascript for filling the title of a Bootstrap modal

I have something like this when I encode in PHP

#task/2013-12-23/517/1+task+for+Some+other+test+apartment+in+Lviv+on+December+23rd+2013

but the decode in Javascript is expecting this

#task/2013-12-23/517/1%20task%20for%20Some%20other%20test%20apartment%20in%20Lviv%20on%20December%2023rd%202013

I dont want to do it like this

var title = "1+task+for+Some+other+test+apartment+in+Lviv+on+December+23rd+2013";
title.replace(/\+/g," ");

Anyone knows a better solution?

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
ppollono
  • 3,421
  • 1
  • 19
  • 29
  • decodeURI or decodeURIComponent – Voonic Dec 18 '13 at 15:59
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Glavić Dec 18 '13 at 16:02
  • 1
    I don't think there's a better solution than replacing the + character. The thing is that PHP's `urlencode` follows a different standard. You could try using `rawurlencode` on the PHP side to get `%20`... – nice ass Dec 18 '13 at 16:07
  • Yes, I dont want to replace "+" for " " , I want to use decodeURIComponent in Javascript – ppollono Dec 18 '13 at 16:24

2 Answers2

2

Try rawurlencode() instead of urlencode

Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
-1

You solution is good. You could use

var decodedTitle = title.replace("+"," "); 

But it is more of the same.

whoacowboy
  • 6,982
  • 6
  • 44
  • 78
  • Read the question please, I know how to replace stuff in a javascript string, thats's not the issue – ppollono Dec 18 '13 at 16:38