希望有人可以帮助解决这个问题。我在以下代码中遇到 PHP Parse错误:语法错误,第9行的/site/folder/path/test/send_email.php中的意外T_CONSTANT_ENCAPSED_STRING :
$email_to = 'email@address.com <script type="text/javascript">
/* <![CDATA[ */
(function(){try{vars,a,i,j,r,c,l=document.getElementById("__cf_email__");a=l.className;if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>'; //the address to which the email will be sent
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
if(mail($email_to, $subject, $message, $headers)){
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
}else{
echo 'failed';// ... or this one to tell it that it wasn't sent
}
答案 0 :(得分:2)
划定这些位:
className;if(a){s=\'\';
您使用单引号启动字符串:
$email_to = '...
所以你需要在字符串中划分任何进一步的单引号。
编辑:我已按如下方式缩进代码:
<?php
$email_to = 'email@address.com <script type="text/javascript">
/* <![CDATA[ */
(function()
{try
{
vars,a,i,j,r,c,l=document.getElementById("__cf_email__");
a=l.className;
if(a)
{
s=\'\';
r=parseInt(a.substr(0,2),16);
for(j=2;a.length-j;j+=2)
{
c=parseInt(a.substr(j,2),16)^r;
s+=String.fromCharCode(c);
}
s=document.createTextNode(s);
l.parentNode.replaceChild(s,l);
}
}
catch(e)
{
}
}
)();
/* ]]> */
</script>';
echo 'Something';
?>
我在其中看不到任何解析错误。
答案 1 :(得分:1)
您需要转义单引号\'
:
$email_to = 'email@address.com <script type="text/javascript">
/* <![CDATA[ */
(function(){try{vars,a,i,j,r,c,l=document.getElementById("__cf_email__");a=l.className;if(a){s=\'\';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.pare ntNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>'; //the address to which the email will be sent