我有一封电子邮件mailto
href链接,当我在主题中使用&
字符时,这会阻止在电子邮件主题行中此&符号后面的任何代码呈现。即Oil & Gas
,只显示为Oil
。
在正常情况下,我只需将&
更改为单词and
,但主题行是通过Wordpress中的帖子标题动态生成的。
有没有人知道如何阻止主题突破,或者换句话说我如何才能让&
显示为文字字符?
代码的剥离版本如下:
<a href="mailto:joe@example.com?subject=Oil&Gas">Apply</a>
虽然在网站的HTML中使用了以下内容:
<a href="mailto:<?php echo $author_email;?>?subject=<?php the_title(); ?>">Apply</a>
任何帮助或想法都很棒,我不确定这是否是html,php或Javascript解决方案?
答案 0 :(得分:2)
您可以使用urlencode
函数转义字符串以便安全使用,如下所示:
<a href="mailto:<?php echo $author_email;?>?subject=<?php echo urlencode(the_title()); ?>">Apply</a>
答案 1 :(得分:1)
您需要urlencode()
标题。
<?php
$title = "Gas&Oil";
?>
<a href="mailto:a@mail.com?subject=<?= urlencode($title); ?>">Apply</a>
此外,由于the_title()
默认情况下会标题,因此您需要使用get_the_title()
,否则urlencode()
将无效。你可以在这里看到这个模拟:
<?php
function the_title() {
echo "Gas & Oil";
}
function get_the_title() {
return "Gas & Oil";
}
?>
<a href="mailto:a@mail.com?subject=<?=urlencode(the_title()); ?>">Apply</a><br> <!-- doesn't work -->
<a href="mailto:a@mail.com?subject=<?=urlencode(get_the_title()); ?>">Apply</a> <!-- works -->
然而,这将编码整个标题,更改您不一定需要编码的其他字符。因此,为避免这种情况,只需替换&
的{{1}}:
%26
答案 2 :(得分:0)
尝试用&#34;%26&#34;替换&符号。或&#34;&amp;&#34; :
<a href="mailto:<?php echo $author_email;?>?subject=<?php str_replace('&', '%26', the_title()); ?>">Apply</a>
答案 3 :(得分:0)
<a href="mailto:<?php echo $author_email;?>?subject=<?php echo str_replace('&','%26',rawurlencode(htmlspecialchars_decode(the_title()))); ?>">Apply</a>
使用PHP组合:str_replace('&','%26',rawurlencode(htmlspecialchars_decode(the_title())));
答案 4 :(得分:0)
问题在于,&字符需要在URL中转义,因为&被视为控制字符。
您可以使用HTML编码对其进行转义。例如,&
是&
字符,而 
是不间断空格。
在JavaScript中,您可以将“ encodeURIComponent”用于主题和正文。然后它将显示电子邮件中的所有特殊字符。
示例:
const emailRequest = {
to: "abc@xyz.com",
cc: "abc@xyz.com",
subject: "Email Request - for <CompanyName>",
body: `Hi All, \r\n \r\n This is my company <CompanyName>
\r\n Thanks
}
const subject = encodeURIComponent(emailRequest.subject.replace("<CompanyName>",'ABC & ** Company'));
const body = encodeURIComponent(emailRequest.body .replace("<CompanyName>", 'ABC & ** Company'));
window.location.href = (`mailto:${emailRequest.to}?cc=${emailRequest.cc}&subject=${subject}&body=${body}`);