Jump to content

encodeURIComponent my best option?


jeffman

Recommended Posts

blush.gifYeah, it's basic. My user is passing a big chunk of text to a PHP script. I need my users to write whatever characters they want. (The data goes into a harmless place, so that's not the problem.)The problem is, when PHP finds the "&" character, it messes up my POST data. (Every entity creates a new variable, etc.) So one solution is to put my text through encodeURIComponent() before I put it into my post string.The results seem fine. When I send it back to the browser and decode it, no problem. (So far.)But this function does SO much more than I need, when all I want is to encode the "&".Am I better off using str.replace() and just nailing the "&"?Is the speed difference inconsequential? Are there other drawbacks I'm not aware of?
Link to comment
Share on other sites

If you're actually encoding the text precisely for a query string, I don't see why you shouldn't use encodeURIComponent. I suppose you're using AJAX, because otherwise the browser would do the encoding for any form fields.I don't have any specific information on the functions, but unless you're doing a whole lot of looping, using either of them isn't going to change the speed noticeably.

Link to comment
Share on other sites

If the ampersand is the only thing you're worried about then you might as well just replace that one character. If you have more than one character then you might as well use an escape function if you also want to catch plusses or things like that. Using string.replace would be faster than an escape function, but not a lot.

Link to comment
Share on other sites

Yeah, I should have said it's AJAX. Maybe that's why I never noticed it before. And no big looping. On the other hand, a typical string might be 20-30KB.From a practical standpoint, I get results in an instant.So maybe my interest is academic more than anything.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...