gpt4 book ai didi

php - 帖子中的空格和特殊字符问题

转载 作者:行者123 更新时间:2023-12-01 04:45:43 25 4
gpt4 key购买 nike

在使用此 AJAX 函数的 POST 操作期间,我遇到了空格和特殊字符的问题:

function comments(id,postId,user_id) {
var user_comments = encodeURIComponent($("#commentsId_"+id).val());
if(user_comments!=''){
var img = $("#img_"+id).val();
var username = "<?php echo $this->session->userdata('username'); ?>"
$.ajax({
type: "POST",
url: "<?php echo base_url() ?>social/userscomment/" +user_id+"/"+postId+"/"+user_comments,
data:{ user_comments : user_comments},
success: function(data) {
$("#commentsId_"+id).val('');
var $sparkLines = $('.comments_body_'+id);
$("#comments_add_"+id).append('<div id="id' + ($sparkLines.length + 1) + '" class="comments_body_"'+id+'><div class="feed-element comments_body_"'+id+'><a class="pull-left"><strong>'+username+' <i class="fa fa-comment-o"></i></strong></a><div class="media-body">'+user_comments+'<br></div><div class="col-lg-12"><a id="span_'+postId+'" onclick="callajaxcommcool('+postId+')" class="btn btn-xs btn-white"><i class="fa fa-star"></i><span id="span1_'+postId+'" style="display:none;">1</span> Cool </a></div></div></div></div>');
}
});
}
}

PHP Controller :

public function userscomment($comment_id,$post_id,$user_comments){

$this->load->model('comments');

$user_comments = utf8_decode(trim(mysql_real_escape_string($_POST['user_comments'])));

if(!empty($user_comments)){

$data = array("user_id" => $this->session->userdata('user_id'),
"comment_user_id" => $comment_id,
"comments" => $user_comments,
"post_id" => $post_id,
"username" => $this->session->userdata('username')
);
$this->comments->insertComments($data);

//logged user
$userRow = $this->register->get_login($this->session->userdata('user_id'));
redirect('social/userprofile/'.$userRow[0]['username']);
}
}

当用户发表诸如“a b c d”之类的评论时, View 中显示的结果是:a%20b%20c%20d,如果用户尝试编写像 € 这样的特殊字符,我得到的结果是 %E2%82%交流

如何防止这个问题?

编辑:

解决了AJAX成功函数中的问题,我错过了

'+decodeURIComponent(user_comments)+'

success: function(data) { 
$("#commentsId_"+id).val('');
var $sparkLines = $('.comments_body_'+id);
$("#comments_add_"+id).append('<div id="id' + ($sparkLines.length + 1) + '" class="comments_body_"'+id+'><div class="feed-element comments_body_"'+id+'><a class="pull-left"><strong>'+username+' <i class="fa fa-comment-o"></i></strong></a><div class="media-body">'+decodeURIComponent(user_comments)+'<br></div><div class="col-lg-12"><a id="span_'+postId+'" onclick="callajaxcommcool('+postId+')" class="btn btn-xs btn-white"><i class="fa fa-star"></i><span id="span1_'+postId+'" style="display:none;">1</span> Cool </a></div></div></div></div>');}

Edit2:我已经执行了代码,因为encodeURIComponent不允许这个字符 ~!*()'",我使用了替换功能来绕过这个限制,希望能有所帮助。

function subcomments(id,postId,user_id) {
var user_comments = encodeURIComponent($("#commentsId_"+id).val()).replace(/\-/g, "%2D").replace(/\_/g, "%5F").replace(/\./g, "%2E").replace(/\!/g, "%21").replace(/\~/g, "%7E").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
if(user_comments!=''){
var img = $("#img_"+id).val();
var username = "<?php echo $this->session->userdata('username'); ?>"
$.ajax({
type: "POST",
url: "<?php echo base_url() ?>social/userscommentcool/" +user_id+"/"+postId+"/"+user_comments,
data:{ user_comments : user_comments },
success: function(data) {
$("#commentsId_"+id).val('');
var $sparkLines = $('.comments_body_'+id);
$("#comments_add_"+id).append('<div id="id' + ($sparkLines.length + 1) + '" class="comments_body_"'+id+'><div class="feed-element comments_body_"'+id+'><a class="pull-left"><strong>'+username+' <i class="fa fa-comment-o"></i></strong></a><div class="media-body">'+decodeURIComponent(user_comments)+'<br></div><div class="col-lg-12"><a id="span_'+postId+'" onclick="callajaxcommcool('+postId+')" class="btn btn-xs btn-white"><i class="fa fa-star"></i><span id="span1_'+postId+'" style="display:none;">1</span> Cool </a></div></div></div></div>');
}
});
}
}

最佳答案

在对象中传递参数时,不要使用 encodeURIComponent。仅当您自己构建 URL 编码字符串时才需要它。 jQuery 自动对对象中的参数进行编码,结果是它被编码了两次。所以就这样做:

var user_comments = $("#commentsId_"+id).val();

关于php - 帖子中的空格和特殊字符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30083670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com