开发者

url decodeing dash and underscore

I am attempting to recommend a human readable URL structure for an Ajax site. I'd love to use either a dash or an underscore to represent multiple words.

So for example: http://www.mysite/Kent-Smith/

The challenge is that we have a bunch of multiple word keywords in the system that we are filtering by url开发者_如何学C that might have either dashes or underscores. (I guess some financial tickers in our system do use underscores).

Am I stuck on the decoding side of things? Do I have to use %20?

thanks!


What I should think you would do is:

  1. Have a designated "escape" character - \ (backslash) is common for this
  2. When encoding, first replace all \ with \\ and then all - with \-, and then all spaces with dashes. (order is important here!)
  3. When decoding, first replace all dashes that are not preceded with \ with a space, then all \- with - then all \\ with \.

For example: say your name is Jo\hn Smith-Jones (I know that it's unlikely for someone to have a backslash in the middle of their name, but...)

Then you encode: replace \ with \\: Jo\\hn Smith-Jones

Now replace - with \-: Jo\\hn Smith\-Jones

Now replace spaces with dashes: Joh\\n-Smith\-Jones (end result)

This does lead to strange backslashes in the middle of the encoded results, but only if there was punctuation in the first place, so that would be relatively uncommon.

Wait, one more thing I just thought of. Backslashes might not work for URLs, so maybe use a different character - process would still be exactly the same, just replace backslash with whatever else.

Happy Hacking!


I suggest you use spaces, encoding them as + in the URL, and leaving hyphens intact:

http://example.com/John+Smith-Jones/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜