简体   繁体   English

如何在 HTML 属性值的 URL 中编码“&”?

[英]How do I encode "&" in a URL in an HTML attribute value?

I'd like to make a URL click able in the email app.我想在电子邮件应用程序中点击 URL。 The problem is that a parameterized URL breaks this because of "&" in the URL.问题是参数化的 URL 由于 URL 中的“&”而破坏了这一点。 The body variable below is the problem line.下面的 body 变量是问题行。 Both versions of "body" are incorrect.两个版本的“body”都不正确。 Once the email app opens, text stops at "...link:".电子邮件应用程序打开后,文本会停在“...link:”处。 What is needed to encode the ampersand?需要什么来编码&符号?

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&param=0'>click me</a>"; //original
NSString *body = @"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&#38;param=0'>click me</a>"; //have also tried &amp;
NSString *encodedBody = 
[body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString *formattedURL = [NSString stringWithFormat: @"mailto:myname@somedomain.com?subject=%@&body=%@", encodedSubject, encodedBody];
NSURL *url = [[NSURL alloc] initWithString:formattedURL];
[[UIApplication sharedApplication] openURL:url];

URL 编码标准中的 HEX 符号为 %26

我一直在使用-[NSString gtm_stringByEscapingForURLArgument] ,它在Google Toolbox for Mac 中提供,特别是在GTMNSString+URLArguments.hGTMNSString+URLArguments.m 中

you can simply use CFURLCreateStringByAddingPercentEscapes with CFBridgingRelease for ARC support你可以简单地使用CFURLCreateStringByAddingPercentEscapesCFBridgingRelease来支持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));

您可以使用字符的十六进制表示,在本例中为 %26。

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 also has an ampersand, which you have not encoded.您插入编码正文的格式有一个&符号,您尚未对其进行编码。 Tack the unencoded string onto it instead, and encode them (using stringByAddingPercentEscapesUsingEncoding ) together.将未编码的字符串添加到其上,并将它们编码在一起(使用stringByAddingPercentEscapesUsingEncoding )。

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;
<a href='http://somewhere.com/two.woa/wa?id=000&#38;param=0'>click me</a>

Is correct, although '&amp;'是正确的,虽然 '&amp;' is more commonly used than '&#38;'比“&#38;”更常用or '&#x2C;'.或“&#x2C;”。

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.如果 'stringByAddingPercentEscapesUsingEncoding' 方法按照它在罐头上所说的那样做,它应该可以工作(*),但是 NSString 文档看起来有点不清楚究竟哪些字符被转义了。 Check what you are ending up with, the URL should be something like:检查你最终得到什么,URL 应该是这样的:

mailto:bob@example.com?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.如果“stringByAddingPercentEscapesUsingEncoding”未将“<”转义为“%3C”,则可能是问题所在。 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.如前所述, ?body=... 不是一个可靠的功能。

In any case, you shouldn't expect the mailer to recognise the HTML and try to send an HTML mail;在任何情况下,您都不应该期望邮件程序识别 HTML 并尝试发送 HTML 邮件; very few will do that.很少有人会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM