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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题