weixin_39422902 2017-12-25 09:35 采纳率: 33.3%
浏览 893
已结题

求高手把本段php译成C#

<?php
$isPartial = empty($_POST['partial']) ? false : true;

// A preset set of messages
$messages = array(
    'required' => 'The field %s is required',
    'invalid' => 'The field %s is invalid',
    'errors' => 'Please fix the errors in the form to continue',
    'generic' => 'An error has occurred and your message has not been delivered. Try later %s',
    'short' => 'The value of the field %s is too short. It must have at least %d characters',
    'success' => 'Thank you for your message %s. It has been successfully sent'
);

// The result of the request
$result = array(
    'status' => '',
    'message' => '',
    'info' => []
);

// Check Full name
if (!$isPartial && $_POST['name'] === '') {
    $result['info'][] = array(
        'field' => 'name',
        'message' => sprintf($messages['required'], 'Full name')
    );
} else if ((!$isPartial || isset($_POST['name'])) && strlen($_POST['name']) <= 3) {
    $result['info'][] = array(
        'field' => 'name',
        'message' => sprintf($messages['short'], 'Full name', 4)
    );
}

// Check Email
if (!$isPartial && $_POST['email'] === '') {
    $result['info'][] = array(
        'field' => 'email',
        'message' => sprintf($messages['required'], 'Email')
    );
} else if ((!$isPartial || isset($_POST['email'])) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $result['info'][] = array(
        'field' => 'email',
        'message' => sprintf($messages['invalid'], 'Email')
    );
}

// Check Subject
if (!$isPartial && $_POST['subject'] === '') {
    $result['info'][] = array(
        'field' => 'subject',
        'message' => sprintf($messages['required'], 'Subject')
    );
} else if ((!$isPartial || isset($_POST['subject'])) && strlen($_POST['subject']) <= 3) {
    $result['info'][] = array(
        'field' => 'subject',
        'message' => sprintf($messages['short'], 'Subject', 4)
    );
}

// Check Message
if (!$isPartial && $_POST['message'] === '') {
    $result['info'][] = array(
        'field' => 'message',
        'message' => sprintf($messages['required'], 'Message')
    );
} else if ((!$isPartial || isset($_POST['message'])) && strlen($_POST['message']) <= 3) {
    $result['info'][] = array(
        'field' => 'message',
        'message' => sprintf($messages['short'], 'Message', 4)
    );
}

if (!empty($result['info'])) {
    $result['status'] = 'error';
    $result['message'] = $messages['errors'];
} else {
    /*
     * If you have a SMTP server on your computer uncomment
     * these lines and remove the `if(true) {` line.
     *
     *
     * if (mail(
     *    'youremail@domain.com',
     *    htmlentities($_POST['subject']),
     *    htmlentities($_POST['message']),
     *    'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'
     * )) {
     */
    if (true) {
        $result['status'] = 'success';
        if ($isPartial) {
            $result['message'] = '';
        } else {
            // Sorry if the htmlentities function sounds unfamiliar to you,
            // but as a programmer I just can leave this statement subject to XSS attacks
            $result['message'] = sprintf($messages['success'], htmlentities($_POST['name']));
        }
    } else {
        $result['status'] = 'error';
        $result['message'] = sprintf($messages['generic'], htmlentities($_POST['name']));
    }
}

echo json_encode($result);
  • 写回答

4条回答

  • guizhong6248 2017-12-25 09:40
    关注

    // A preset set of messages
    $messages = array(
    'required' => 'The field %s is required',
    'invalid' => 'The field %s is invalid',
    'errors' => 'Please fix the errors in the form to continue',
    'generic' => 'An error has occurred and your message has not been delivered. Try later %s',
    'short' => 'The value of the field %s is too short. It must have at least %d characters',
    'success' => 'Thank you for your message %s. It has been successfully sent'
    );

    // The result of the request
    $result = array(
    'status' => '',
    'message' => '',
    'info' => []
    );

    // Check Full name
    if (!$isPartial && $_POST['name'] === '') {
    $result['info'][] = array(
    'field' => 'name',
    'message' => sprintf($messages['required'], 'Full name')
    );
    } else if ((!$isPartial || isset($_POST['name'])) && strlen($_POST['name']) <= 3) {
    $result['info'][] = array(
    'field' => 'name',
    'message' => sprintf($messages['short'], 'Full name', 4)
    );
    }

    // Check Email
    if (!$isPartial && $_POST['email'] === '') {
    $result['info'][] = array(
    'field' => 'email',
    'message' => sprintf($messages['required'], 'Email')
    );
    } else if ((!$isPartial || isset($_POST['email'])) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $result['info'][] = array(
    'field' => 'email',
    'message' => sprintf($messages['invalid'], 'Email')
    );
    }

    // Check Subject
    if (!$isPartial && $_POST['subject'] === '') {
    $result['info'][] = array(
    'field' => 'subject',
    'message' => sprintf($messages['required'], 'Subject')
    );
    } else if ((!$isPartial || isset($_POST['subject'])) && strlen($_POST['subject']) <= 3) {
    $result['info'][] = array(
    'field' => 'subject',
    'message' => sprintf($messages['short'], 'Subject', 4)
    );
    }

    // Check Message
    if (!$isPartial && $_POST['message'] === '') {
    $result['info'][] = array(
    'field' => 'message',
    'message' => sprintf($messages['required'], 'Message')
    );
    } else if ((!$isPartial || isset($_POST['message'])) && strlen($_POST['message']) <= 3) {
    $result['info'][] = array(
    'field' => 'message',
    'message' => sprintf($messages['short'], 'Message', 4)
    );
    }

    if (!empty($result['info'])) {
    $result['status'] = 'error';
    $result['message'] = $messages['errors'];
    } else {
    /*
    * If you have a SMTP server on your computer uncomment
    * these lines and remove the if(true) { line.
    *
    *
    * if (mail(
    * 'youremail@domain.com',
    * htmlentities($_POST['subject']),
    * htmlentities($_POST['message']),
    * 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'
    * )) {
    */
    if (true) {
    $result['status'] = 'success';
    if ($isPartial) {
    $result['message'] = '';
    } else {
    // Sorry if the htmlentities function sounds unfamiliar to you,
    // but as a programmer I just can leave this statement subject to XSS attacks
    $result['message'] = sprintf($messages['success'], htmlentities($_POST['name']));
    }
    } else {
    $result['status'] = 'error';
    $result['message'] = sprintf($messages['generic'], htmlentities($_POST['name']));
    }
    }

    echo json_encode($result);

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器