简体   繁体   English

避免将空格从mailto 链接更改为iPhone 中的加号

[英]Avoid space change into plus sign in iPhone from mailto link

I have a php code that renders a mailto: link我有一个 php 代码,它呈现一个mailto:链接

echo "<a href='mailto:?'". http_build_query([
   'subject' => 'this is a subject',
   'body' => 'this is a body',
])."'>Link</a>";

In my desktop browser, when I clicked the link the browser goes to GMail and the mail body is rendered nicely.在我的桌面浏览器中,当我单击链接时,浏览器会转到 GMail,并且邮件正文呈现得很好。

But when I use my iPhone, the compose app shows "+" instead of spaces.但是当我使用 iPhone 时,撰写应用程序会显示“+”而不是空格。

How can I avoid this?我怎样才能避免这种情况?

This is standard URI code.这是标准的 URI 代码。 Spaces are expected to be transformed to something else than a space.预计空间将被转换为空间以外的其他东西。 The most common character is the '+', some people prefer to use %20.最常见的字符是“+”,有些人更喜欢使用 %20。

When you decode a URI, it automatically replaces the '+' back to spaces.当您解码 URI 时,它会自动将“+”替换回空格。 So your code is probably missing that one call...所以你的代码可能缺少那个电话......

You probably want to look at this function:你可能想看看这个函数:

http://us1.php.net/manual/en/function.urldecode.php http://us1.php.net/manual/en/function.urldecode.php


According to scrowler comment, try to put %20 instead of spaces to see what happens:根据 scrowler 评论,尝试放置 %20 而不是空格,看看会发生什么:

echo "<a href='mailto:?'". http_build_query([
   'subject' => 'this%20is%20a%20subject',
   'body' => 'this%20is%20a%20body',
])."'>Link</a>";

For building the mailto http query string to be compatible with Apple mail and not show spaces as + plus signs you need to use the encoding_type param in the http_build_query function and set it to constant PHP_QUERY_RFC3986 so that spaces are encoded with the %20 instead of + .要构建 mailto http 查询字符串以与 Apple 邮件兼容并且将空格显示为+加号,您需要在http_build_query函数中使用encoding_type参数并将其设置为常量PHP_QUERY_RFC3986以便使用%20而不是+对空格进行编码.

Build the mailto query string:构建 mailto 查询字符串:

$mailToParams = http_build_query(
    [
        'subject' => "The Subject!",
        'body' => "Here's the body of the message. \n\nAdd in a couple new lines."
    ],
    null,
    '&',
    PHP_QUERY_RFC3986
);

Add it to your link:将其添加到您的链接中:

<a href="mailto:info@example.com?<?php echo $mailToParams; ?>">Mail Me</a>

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

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