PHP 奇怪的语法错误行 1 设置邮件黑猩猩


PHP Strange Syntax Error Line 1 Setting Up Mailchimp

我有 html 发送一个 POST 请求,该请求到达 php 代码来处理请求......我收到一个奇怪的错误,说第 1 行存在语法错误

解析错误:语法错误,第 1 行/home/content/31/9275231/html/subscribe.php 中的意外T_FUNCTION

但是,我在第 1 行没有看到任何错误。

这是代码(我隐藏了我的API密钥信息)

<?php
function isValidEmail( $email = null )
{
    return preg_match( "/^
    ['d'w'/+!=#|$?%{^&}*`'~-]
    ['d'w'/'.+!=#|$?%{^&}*`'~-]*@
    [A-Z0-9]
    [A-Z0-9.-]{1,61}
    [A-Z0-9]'.
    [A-Z]{2,6}$/ix", $email );
}
/* Check if email has been posted */
if ( !isset($_POST['email']) ) die();
/* Validate email */
if ( isValidEmail($_POST['email']) ) {
require_once('./MCAPI.class.php');  
// **************************************************************** //
    // Enter your API Key from http://admin.mailchimp.com/account/api/
    $api = new MCAPI('apikey');
    // Enter your list's unique id from http://admin.mailchimp.com/lists/
    // (click the "settings", the unique id is at the bottom of the page) 
    $list_id = 'list_unique_id';
// **************************************************************** //
if($api->listSubscribe($list_id, $_POST['email'], '') === true) {
    echo 'successful';
}else{
    echo 'Error: ' . $api->errorMessage;
    }
} 
else {
    echo 'invalid_email';
}

另一件奇特的事情:我注意到当我在textmate中打开这个php代码时,它看起来很好,但是当我在vim中打开它时,所有代码都显示在一行中,带有奇怪的"^M"字符,新行应该是...有什么想法吗?

奇怪的^M字符是Windows/DOS行尾。使用此选项将它们替换为 Unix 行尾:

:%s/^V^M/'r/g

更多信息在这里: http://grx.no/kb/2008/11/17/remove-windows-line-endings-in-vim/

检查文本编辑器中的选项,看看是否可以将换行符设为 LF 而不是 CR(或两者均为 CR 后跟 LF)。 正在发生的事情是你的换行符只是CR,而PHP解释器正在寻找换行符的LF,所以它会把你的代码读成一个大行。