在PHP

时间:2018-05-01 06:00:59

标签: javascript php jquery forms

在提交表单和验证开始之前,有没有办法将左右单引号转换为撇号?

我问的原因是我的表单在所有平台上都能正常运行,但iPhone用户有时使用错误的字符并输入左或右单引号。由于我的表单验证输入的数据并且必须准确,因此左侧单引号被视为与DB表中的撇号相比的错误,并且不接受该信息。

如果我可以在用户提交撇号时转换这些字符,那么即使在iPhone上也能正常工作。我会非常感激任何见解,我尝试了一些像htmlspecialchar这样的项目,但似乎并没有帮助。

另外我应该注意,当使用iPhone并在ios11上的智能键盘布局中输入coorect撇号时,我能够成功通过表单验证。这似乎是苹果的一个已知问题,但他们还没有纠正这个问题,我相信如果我们能找到答案,我希望这个解决方案对社区中的许多人都有益。

相关表单字段为:

  <input type="text" name="last_name" id="last_name" value="<?php echo htmlspecialchars(stripslashes(isset($fields['last_name'])) ? $fields['last_name'] : '') ?>" >

如何设置此表单字段以使用php或jquery将左右单引号转换为表单提交中的撇号?

更新

@fubar今晚你是我的救世主。第一个解决方案很有效。但是我如何添加第二个选项呢?

function cv(&$fields, &$errors) {

  // Check args and replace if necessary
  if (!is_array($fields))     $fields = array();
  if (!is_wp_error($errors))  $errors = new WP_Error;

  // Check for form submit
  if (isset($_POST['submit'])) {

    // Get fields from submitted form
    $fields = cv_get_fields();

    // Validate fields and produce errors
    if (cv_validate($fields, $errors)) {
       // If successful, display a message

      $Id=$fields['login'];
      $First=$fields['first_name'];
      $Last=$fields['last_name'];
      global $wpdb;
      $user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM users WHERE login = %s", $Id ) );
      $userquery = $wpdb->get_results($user->login);

      $Id=$fields['login'];

        if ( !username_exists ($Id)) {
          $Id=$fields['login'];
          $access = $wpdb->get_row( $wpdb->prepare( "SELECT access FROM table WHERE ID = %s", $Id ) );

          foreach ($access as $accessquery) {
            if ($accessquery == lvl2) {
              $_SESSION['mem_data']=$fields;
              header("Location: https://example.com/lvl2-register/");
            }
            if ( is_null ($accessquery)) {
               $_SESSION['mem_data']=$fields;
              header("Location: https://example.com/lvl1-register/");
            }

          }
        }
        elseif ( username_exists ($Id)) {
            header("Location: https://example.com/already-registered/");
        }
    }
  }


  // Santitize fields
  cv_sanitize($fields);

  // Generate form
  cv_display_form($fields, $errors);
}

function cv_sanitize(&$fields) {
  $fields['login']   =  isset($fields['login'])  ? sanitize_user($fields['login']) : '';
  $fields['first_name']   =  isset($fields['first_name'])  ? sanitize_text_field($fields['first_name']) : '';
  $fields['last_name']    =  isset($fields['last_name'])   ? sanitize_text_field($fields['last_name']) : '';
}

function cv_display_form($fields = array(), $errors = null) {

  // Check for wp error obj and see if it has any errors  
  if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) {

    // Display errors
    ?>
 <div class="step1-form" style="display:block;margin:0 auto;text-align:left;max-width:1080px;">
<?php
    foreach ($errors->get_error_messages() as $key => $val) {
      ?><p>
        <?php echo $val; ?>
      </p><?php
    }
    ?><?php
  }

  // Display form
  ?>
</div>
  <div class="step1-form" style="display:block;margin:0 auto;text-align:left;max-width:1080px;">

  <h1 class="til-postheader entry-title">User Registration: Step 1 of 2</h1>

    <h4>Prior to registering for this website you must first verify Your Membership.<h4>




<div id="login" style="max-width:175px;width:100%;margin:10px;">
  <form action="" method="post">
    <div>
      <label for="first_name">First Name:</label><br>
      <input type="text" name="first_name" id="first_name" value="<?php echo (isset($fields['first_name']) ? $fields['first_name'] : '') ?>" >
    </div>
    <br>
    <div>
      <label for="last_name">Last Name:</label><br>
      <input type="text" name="last_name" id="last_name" value="<?php echo htmlspecialchars(stripslashes(isset($fields['last_name'])) ? $fields['last_name'] : '') ?>" >

    </div>
<br>
    <div>
        <a data-fancybox data-src="#ID" href="javascript:;" style="outline:none;border:0px;text-decoration:none;" tabindex="-1"><span style="width:21px;float:right;color:#ffffff;background:#0a4b73;text-align:center;line-height:21px;border-radius:50%;" tabindex="-1">?</span></a><label for="login">Member ID:</label><br>
        <input type="text" name="login" id="login" value="<?php echo (isset($fields['login']) ? $fields['login'] : '') ?>" >
    </div>
    <br>
    <input type="submit" name="submit" value="Verify Membership">
    </form>
</div>

<?php
}

function cv_get_fields() {
  return array(
    'login'   =>  isset($_POST['login'])   ?  $_POST['login']   :  '',
    'first_name'   =>  isset($_POST['first_name'])   ?  $_POST['first_name']        :  '',
    'last_name'    =>  isset($_POST['last_name'])    ?  $_POST['last_name']        :  '',
  );
}

function cv_validate(&$fields, &$errors) {

  // Make sure there is a proper wp error obj
  // If not, make one
  if (!is_wp_error($errors))  $errors = new WP_Error;

  // Validate form data
// Define $Card $First $Last $PIN
  $Id=$fields['login'];
  $First=$fields['first_name'];
  $Last=$fields['last_name'];
  // $Lastname = htmlspecialchars_decode(stripslashes($fields["last_name"]));

 $Lastname = '&lsquo; ‘ - ’ &rsquo;';
 $Lastname = str_replace(['‘', '’'], "'", html_entity_decode(stripslashes($fields["last_name"])));
  global $wpdb;
  $result = $wpdb->get_row( $wpdb->prepare( 'SELECT distinct ID, First, Last, FROM table WHERE ID = %s AND First = "%s" AND Last = "%s", $Id, $First, $Lastname ) );
  if ( is_null ( $result ) ) {
    $errors->add('non_member', 'The information entered does not match our records.');
  }

  if (empty($fields['login']) || empty($fields['first_name']) || empty($fields['last_name'])) {
    $errors->add('field', '');
  }

  // If errors were produced, fail
  if (count($errors->get_error_messages()) > 0) {
    return false;
  }
  $Id=$fields['login'];
  global $wpdb;
  $accessno = $wpdb->get_row( $wpdb->prepare( "SELECT distinct access FROM table WHERE ID = %s", $Id ) );
  foreach ($accessno as $noquery) {
  if ( $noquery == NO) {
    header ('Location: https://example.com/access-denied/');
  }
  } 
  // Else, success!
  return true;
}

// The callback function for the [cv] shortcode
function cv_cb() {
  $fields = array();
  $errors = new WP_Error();

  // Buffer output
  ob_start();

  // Custom verification, go!
  cv($fields, $errors);

  // Return buffer
  return ob_get_clean();
}
add_shortcode('cv', 'cv_cb');

1 个答案:

答案 0 :(得分:1)

这会将原始和编码的单引号转换为撇号。

$value = '&lsquo; ‘ - ’ &rsquo;';

$value = str_replace(['‘', '’'], "'", html_entity_decode($value));

如果要将其应用于所有POST数据,可以使用:

$_POST = array_map(function ($value) {
    return str_replace(['‘', '’'], "'", html_entity_decode($value));
}, $_POST);

修改

如果您替换以下行:

// Get fields from submitted form
$fields = cv_get_fields();

使用:

// Get fields from submitted form
$fields = array_map(function ($value) {
    return str_replace(['‘', '’'], "'", html_entity_decode($value));
}, cv_get_fields());

然后你可以删除以下内容:

$Lastname = '&lsquo; ‘ - ’ &rsquo;';
$Lastname = str_replace(['‘', '’'], "'", html_entity_decode(stripslashes($fields["last_name"])));