objective-c 如何在 HTML 属性值的 URL 中编码“&”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/730101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I encode "&" in a URL in an HTML attribute value?
提问by 4thSpace
I'd like to make a URL click able in the email app. The problem is that a parameterized URL breaks this because of "&" in the URL. The body variable below is the problem line. Both versions of "body" are incorrect. Once the email app opens, text stops at "...link:". What is needed to encode the ampersand?
我想在电子邮件应用程序中点击 URL。问题是参数化的 URL 由于 URL 中的“&”而破坏了这一点。下面的 body 变量是问题行。两个版本的“body”都不正确。电子邮件应用程序打开后,文本会停在“...link:”处。需要什么来编码&符号?
NSString *subject = @"This is a test";
NSString *encodedSubject =
[subject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000¶m=0'>click me</a>"; //original
NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>"; //have also tried &
NSString *encodedBody =
[body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *formattedURL = [NSString stringWithFormat: @"mailto:[email protected]?subject=%@&body=%@", encodedSubject, encodedBody];
NSURL *url = [[NSURL alloc] initWithString:formattedURL];
[[UIApplication sharedApplication] openURL:url];
回答by Konstantinos
the ampersand would be %26 for HEX in URL Encodingstandards
URL 编码标准中的HEX 符号为 %26
回答by Chris Lundie
I've been using -[NSString gtm_stringByEscapingForURLArgument], which is provided in Google Toolbox for Mac, specifically in GTMNSString+URLArguments.hand GTMNSString+URLArguments.m.
我一直在使用-[NSString gtm_stringByEscapingForURLArgument],它在Google Toolbox for Mac 中提供,特别是在GTMNSString+URLArguments.h和GTMNSString+URLArguments.m 中。
回答by Lucero
You can use a hex representation of the character, in this case %26.
您可以使用字符的十六进制表示,在本例中为 %26。
回答by AbdullahDiaa
you can simply use CFURLCreateStringByAddingPercentEscapeswith CFBridgingReleasefor ARCsupport
您可以简单地使用CFURLCreateStringByAddingPercentEscapeswithCFBridgingRelease来支持ARC
NSString *subject = @"This is a test";
// Encode all the reserved characters, per RFC 3986
// (<http://www.ietf.org/rfc/rfc3986.txt>)
NSString *encodedSubject =
(NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)subject,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
回答by Peter Hosey
You use stringByAddingPercentEscapesUsingEncoding, exactly like you are doing.
你使用stringByAddingPercentEscapesUsingEncoding,就像你正在做的那样。
The problem is that you aren't using it enough. The format into which you're inserting the encoded body alsohas an ampersand, which you have not encoded. Tack the unencoded string onto it instead, and encode them (using stringByAddingPercentEscapesUsingEncoding) together.
问题是你用的还不够多。您插入编码正文的格式也有一个&符号,您尚未对其进行编码。将未编码的字符串添加到其上,并将它们(使用stringByAddingPercentEscapesUsingEncoding)一起编码。
回答by zzapper
Example of use of %26 instead of & without this attributes arrived in PHP as an array!
使用 %26 代替 & 没有此属性的示例作为数组到达 PHP!
var urlb='/tools/lister.php?type=101%26ID='+ID; // %26 instead of &
window.location.href=urlb;
回答by bobince
<a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>
Is correct, although ‘&' is more commonly used than ‘&' or ‘,'.
是正确的,虽然 '&' 比“&”更常用 或“,”。
If the ‘stringByAddingPercentEscapesUsingEncoding' method does what it says on the tin, it should work(*), but the NSString documentation looks a bit unclear on which characters exactly are escaped. Check what you are ending up with, the URL should be something like:
如果 'stringByAddingPercentEscapesUsingEncoding' 方法按照它在罐头上所说的那样做,它应该可以工作(*),但是 NSString 文档看起来有点不清楚究竟哪些字符被转义了。检查你最终得到什么,URL 应该是这样的:
mailto:[email protected]?subject=test&body=Link%3A%3Ca%20href%3D%22http%3A//example.com/script%3Fp1%3Da%26amp%3Bp2%3Db%22%3Elink%3C/a%3E
(*: modulo the usual disclaimer that mailto: link parameters like ‘subject' and ‘body' are non-standard, will fail in many situations, and should generally be avoided.)
(*:以通常的免责声明为模,mailto:像“主题”和“正文”这样的链接参数是非标准的,在许多情况下会失败,通常应该避免。)
Once the email app opens, text stops at "...link:".
电子邮件应用程序打开后,文本会停在“...link:”处。
If ‘stringByAddingPercentEscapesUsingEncoding' is not escaping ‘<' to ‘%3C', that could be the problem. Otherwise, it might not be anything to do with escapes, but a deliberate mailer-level restriction to disallow ‘<'. As previously mentioned, ?body=... is not a reliable feature.
如果“stringByAddingPercentEscapesUsingEncoding”未将“<”转义为“%3C”,则可能是问题所在。否则,它可能与转义无关,而是故意禁止“<”的邮件程序级别限制。如前所述, ?body=... 不是一个可靠的功能。
In any case, you shouldn't expect the mailer to recognise the HTML and try to send an HTML mail; very few will do that.
在任何情况下,您都不应该期望邮件程序识别 HTML 并尝试发送 HTML 邮件;很少有人会这样做。

