简体   繁体   English

在以下情况下如何从每个数组元素中删除\\'?

[英]How to remove \' from each array element in following scenario?

I'm taking values from hidden fields present on my form using jQuery. 我正在使用jQuery从表单上存在的隐藏字段中获取值。 Each of these values are present within single quotes like 'enable' . 这些值中的每一个都用单引号引起,例如'enable' Now, I'm creating an array and pushing these values in it. 现在,我正在创建一个数组并将这些值推入其中。 I'm sending this array using javascript to a PHP file. 我正在使用javascript将此数组发送到PHP文件。 I'm sending this array through jQuery AJAX method's url attrubute. 我通过jQuery AJAX方法的url attrubute发送此数组。 The code for it is as follows: 其代码如下:

$(document).ready(function() {
  $(document).on('click', '#disable_url', function (e) {    
    e.preventDefault();

    var items = new Array();

    $("input:checked:not(#ckbCheckAll)").each(function() {
      items.push($(this).val());
    });

    var str = $("#user_filter").serialize();    
    var str = decodeURIComponent(str);

    var user_status = new Array();
    for (var i = 0; i < items.length; i++) {
      user_status.push($("#"+items[i]).val());
    }    

    $.ajax({
      type: "POST",
      url: "manage_users.php?op=disable_bulk_users&items="+items+"&options="+str+"&user_status="+user_status,
      dataType: 'json',  
      success: function(data) {
        var redirect_link = data.href;
        window.location.href = redirect_link;
      }          
    });
  })
});

Now the PHP code snippet from manage_users.php is as follows: 现在来自manage_users.php的PHP代码片段如下:

$request = empty( $_GET ) ? $_POST : $_GET ;
  $op       = $request['op'];
  switch( $op ) {
    case "disable_bulk_users":
   /*I'm using explode here as I'm getting the comma separated list of strings and I want to convert it back into an array*/ 
    $user_ids = explode(',', $request['items']);
    $user_statuses = explode(',', $request['user_status']);
    print_r($user_statuses);
    die;
    break;
  }

If I print the array $user_statuses it prints as follows: 如果我打印数组$user_statuses则打印如下:

Array
(
    [0] => \'enable\'
    [1] => \'enable\'
    [2] => \'enable\'
    [3] => \'enable\'
    [4] => \'enable\'
    [5] => \'enable\'
    [6] => \'disable\'
    [7] => \'disable\'
    [8] => \'disable\'
    [9] => \'disable\'
    [10] => \'disable\'
    [11] => \'disable\'
    [12] => \'disable\'
    [13] => \'disable\'
    [14] => \'disable\'
    [15] => \'disable\'
    [16] => \'disable\'
    [17] => \'disable\'
    [18] => \'disable\'
    [19] => \'disable\'
)

Actually I want to remove the string \\' occuring at the beginning and the string \\' occuring at the end of each array element. 其实我想删除该字符串\\每个阵元的末尾存在的开头和字符串\\存在的”。 Can you help me in getting the clean array after removal of these strings? 删除这些字符串后,您能否帮助我获得干净的阵列? Thanks in advance. 提前致谢。

Try This: 尝试这个:

function mytrim(&$item){
 $item = trim($item, "\'");
}
$array = array("\'test\'", "\'test2\'");

array_walk($array, "mytrim");
print_r($array);//Array ( [0] => test [1] => test2 )

如果要在客户端执行此操作:

user_status.push($("#"+items[i]).val().replace(/'/g,''));

in php, 在php中

$data = array_map('stripslashes', $data);

or if you want to remove the quotes too 或者如果您也想删除引号

$data = array_map('trim', $data, ['\\\''])

Eliminate the "\\'" from the string using string replace. 使用字符串替换从字符串中消除“ \\'”。 $status= str_replace("\\'",'',$user_statuses); $ status = str_replace(“ \\'”,'',$ user_statuses);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM