Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当空格是post data php的最后一个字符时出现的问题_Php_Jquery_Ajax_Encoding_Contenteditable - Fatal编程技术网

当空格是post data php的最后一个字符时出现的问题

当空格是post data php的最后一个字符时出现的问题,php,jquery,ajax,encoding,contenteditable,Php,Jquery,Ajax,Encoding,Contenteditable,我试图让我的流应用程序的版主使用contenteditable、jquery和php直接在观看页面中修改一集的数据。一切都很完美,除非场的末端有空格 正如我发现的,请求被正确发送。但是最后一个空格在服务器端采用两个字符的大小,不会使用phptrim()删除,因此它不再作为空格接收。我所有的数据、页面和服务器都被编码到utf-8中,所以这不是真正的编码问题 使用base64编码,我发现这个字符是“”(包括空格) 我做错什么了吗?基本代码如下,您可以自己尝试: html: <section&g

我试图让我的流应用程序的版主使用contenteditable、jquery和php直接在观看页面中修改一集的数据。一切都很完美,除非场的末端有空格

正如我发现的,请求被正确发送。但是最后一个空格在服务器端采用两个字符的大小,不会使用php
trim()
删除,因此它不再作为空格接收。我所有的数据、页面和服务器都被编码到utf-8中,所以这不是真正的编码问题

使用base64编码,我发现这个字符是“
”(包括空格)

我做错什么了吗?基本代码如下,您可以自己尝试:

html:

<section>
    <span id=kubi_synopsys>Some content and the space: </span>
</section>
$("#synopsys").keydown(function(e){
    if(e.keyCode===13){//press enter
        $("#synopsys").html($("#synopsys").text()) //removing new lines
        //posting data
        //#episode is a <data> tag containing the current episode's info
        $.post("synopsys-set.php",{
            series: $("#episode").data("series"),
            season: $("#episode").data("season"),
            episode: $("#episode").data("episode"),
            synopsys: $("#synopsys").text()
        },"json")
        .success(function(data){
            console.log("success",atob(data.response))
        })
        .error(function(){
            console.log("error")
        })
    }
})
<?php
//acquisition
$series = isset($_POST['series'])? (int)$_POST['series'] :0;
$season = isset($_POST['season'])? (int)$_POST['season'] :0;
$episode = isset($_POST['episode'])? $_POST['episode'] :0;
$version = isset($_POST['version'])? htmlspecialchars(trim($_POST['version'])) :'';
$synopsys = isset($_POST['synopsys'])? htmlspecialchars(trim($_POST['synopsys'])) :'';

header('Content-type: application/json');
echo json_encode(array('response'=> base64_encode($synopsys)));

一些内容和空间:
Javascript:

<section>
    <span id=kubi_synopsys>Some content and the space: </span>
</section>
$("#synopsys").keydown(function(e){
    if(e.keyCode===13){//press enter
        $("#synopsys").html($("#synopsys").text()) //removing new lines
        //posting data
        //#episode is a <data> tag containing the current episode's info
        $.post("synopsys-set.php",{
            series: $("#episode").data("series"),
            season: $("#episode").data("season"),
            episode: $("#episode").data("episode"),
            synopsys: $("#synopsys").text()
        },"json")
        .success(function(data){
            console.log("success",atob(data.response))
        })
        .error(function(){
            console.log("error")
        })
    }
})
<?php
//acquisition
$series = isset($_POST['series'])? (int)$_POST['series'] :0;
$season = isset($_POST['season'])? (int)$_POST['season'] :0;
$episode = isset($_POST['episode'])? $_POST['episode'] :0;
$version = isset($_POST['version'])? htmlspecialchars(trim($_POST['version'])) :'';
$synopsys = isset($_POST['synopsys'])? htmlspecialchars(trim($_POST['synopsys'])) :'';

header('Content-type: application/json');
echo json_encode(array('response'=> base64_encode($synopsys)));
$(“#synopsys”).keydown(函数(e){
如果(e.keyCode==13){//按enter键
$(“#synopsys”).html($(“#synopsys”).text()//删除新行
//过帐数据
//#插曲是包含当前插曲信息的标记
$.post(“synopsys set.php”{
系列:$(“#集”)。数据(“系列”),
季节:$(“#集”)。数据(“季节”),
插曲:$(“插曲”)。数据(“插曲”),
synopsys:$(“#synopsys”).text()
}“json”)
.成功(功能(数据){
console.log(“成功”,atob(data.response))
})
.错误(函数(){
console.log(“错误”)
})
}
})
php:

<section>
    <span id=kubi_synopsys>Some content and the space: </span>
</section>
$("#synopsys").keydown(function(e){
    if(e.keyCode===13){//press enter
        $("#synopsys").html($("#synopsys").text()) //removing new lines
        //posting data
        //#episode is a <data> tag containing the current episode's info
        $.post("synopsys-set.php",{
            series: $("#episode").data("series"),
            season: $("#episode").data("season"),
            episode: $("#episode").data("episode"),
            synopsys: $("#synopsys").text()
        },"json")
        .success(function(data){
            console.log("success",atob(data.response))
        })
        .error(function(){
            console.log("error")
        })
    }
})
<?php
//acquisition
$series = isset($_POST['series'])? (int)$_POST['series'] :0;
$season = isset($_POST['season'])? (int)$_POST['season'] :0;
$episode = isset($_POST['episode'])? $_POST['episode'] :0;
$version = isset($_POST['version'])? htmlspecialchars(trim($_POST['version'])) :'';
$synopsys = isset($_POST['synopsys'])? htmlspecialchars(trim($_POST['synopsys'])) :'';

header('Content-type: application/json');
echo json_encode(array('response'=> base64_encode($synopsys)));
尝试删除:

$("#synopsys").html($("#synopsys").text()) //removing new lines
因为这不是必需的。无论如何,当
发布
数据时,您正在
#synopsys
上使用
text()

您可以使用
trim()
删除所有前导空格和尾随空格

您可以使用
regex
删除所有空格字符(制表符、换行符等)


正如@AnkitPise所说,
preg__替换('/[^a-z0-9.-]/','','$string)
,将删除所有不必位于服务器端正常文本中的字符,因此这就是我想要的

另外,正如@Tushar所说,
String.trim()
在客户端解决了这个问题。没有空间,没有奇怪的字符


然而,即使我的问题解决了,有人知道它为什么会出现吗?因为有些字段不能像synopsys(纯文本)那样严格,并且可能包含一些外来字符,在这种情况下,使用正则表达式是不可能的。

尝试删除不需要的字符吗<代码>预替换(“/[^A-Za-z0-9]/”,“$string”)
@AnkitPise好主意,添加一些重音字母、圆点等,这就是我想要的。SolvedSure,这是一个技巧,但仅限于客户端。我也希望对服务器端执行同样的操作。@Pierretasel一旦从客户端修剪完所有空格,就不需要在服务器端再次执行此操作。我希望能够使用不同的源来更改该集的数据,而不仅仅是通过浏览器,而且所有的处理都应该在服务器端针对我的特定案例进行