在PHP中存储包含特殊字符的内容
我有一个在PHP中存储包含特殊字符的内容,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有一个add\u post表格,其中包括post\u title、post\u content和date发布内容可能包含特殊字符和HTML元素 存储到post_内容中存在问题 示例字符串: 健身房围绕着这个独特的因素,称为“社区” 如果我尝试将该字符串插入数据库表,则会插入以下字符串,例如: 健身房围绕着一个独特的因素,即 由于符号(“””)的原因,我无法插入所有数据 我只想删除这些符号,以便能够添加所有post\u内容数据 这就是我尝试过的: $post_content=$_POST['p
add\u post
表格,其中包括post\u title
、post\u content
和date
<代码>发布内容可能包含特殊字符和HTML元素
存储到post_内容中存在问题
示例字符串:
健身房围绕着这个独特的因素,称为“社区”
如果我尝试将该字符串插入数据库表,则会插入以下字符串,例如:
健身房围绕着一个独特的因素,即
由于符号(“
”
”
)的原因,我无法插入所有数据
我只想删除这些符号,以便能够添加所有post\u内容
数据
这就是我尝试过的:
$post_content=$_POST['post_content'];
$post_content=addslashes($post_content);
$post_content=htmlentities($post_content);
使用mysql\u real\u escape\u字符串 比如说
$name='abc,cf';
$name=mysql_real_escape_string($name);
我只想删除这些符号,以便能够添加所有post_内容数据
回答这个问题(这与问题标题i quess不同),您实际上可以在控制器it自用功能中保存到数据库之前删除特殊字符
public function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
$string =preg_replace('/-/', ' ', $string); // Replace hyphens with spaces.
$string =preg_replace("/\s+/", " ", $string); //remove extra spaces
return $string;
}
public function somFun(){
$post_content=$_POST['post_content'];
$post_content=$this->clean(post_content);
//proceed to modal function for saving in database
}
编辑
仅删除单引号和双引号的步骤
public function somFun(){
$post_content=$_POST['post_content'];
$post_content=str_replace(array("'", "\"","“","”","’"), "", $post_content );
$post_content=preg_replace("/\s+/", " ", $post_content); /*remove extra spaces*/
//proceed to modal function for saving in database
}
您可以使用
您可以始终只使用这些值,那么这些值中包含的内容就无关紧要了。如果您使用的是CodeIgniter,则存在相关问题,您有两种选择:
使用查询构建:数据将由PDO转义
$sql = "INSERT IGNORE INTO my_table(lat, lng, date, type) VALUES (?,?,?,?);";
$this->db->query($sql, array($data['lat'], $data['lng'], $data['date'], $data['type']));
使用db->escape()方法:显式转义数据
$this->db->query("INSERT IGNORE INTO my_table(lat, lng, date, type) VALUES ('" . $this->db->escape($data['lat']) . "', '" . $this->db->escape($data['lng']) . "', '" . $this->db->escape($data['date']$this->db->escape . "', '" . $this->db->escape($data['type']) . "')");
希望这有帮助 尝试使用form helper插入数据,然后像这样尝试设置值('description')
或使用此htmlspecialchars($POST['description'))
我使用此命令将数据保存到数据库中,并使用此命令显示类似于htmlspecialchars\u解码($description)
utf8问题
“”
不是普通的ascii字符。如果它们被错误地馈送到字符集utf8
列,则字符串可能会被截断。这就是正在发生的事情吗?你得到了所有的文字,但不包括其中一个引号字符?如果是这样,那么关于修复代码和/或模式的解释和提示是——搜索“truncate”
可能的问题是,输入的字节是用拉丁语编码的,而不是utf8,但连接被指定为UTF-8,表/列是column is字符集utf8
(或utf8mb4)
由于PHP或MySQL无法将这些字符识别为引号,因此许多其他建议都无效。如果您只想删除
<?php
$html="a“”’b";
$res = preg_replace('/[^\x20-\x7E]/','', $html);
echo $res;
?>
输出ab我不知道为什么要使用mysqli\u real\u escape\u字符串,或者为什么要将“”插入到数据库中,我使用codeigniter将文本插入到我的数据库中,没有问题,跟踪这一点 在控制器中
$coments = $this->input->post('comentario', true);
$query = $obj->Insertcoments($coments);
在模型中
function Insert($coments) {
$data = array(
'mensaje' => $coments);
$this->db->insert('mensaje', $data);
}
我用一个技巧做了同样的事情
$html = base64_encode($this->input->post->('key'));
并在显示时间解码它
它将解决所有特殊字符问题查看如何将数据插入数据库?您正在使用哪个数据库?数据库中的哪个字符集?您的问题不清楚,也没有显示任何研究成果。您在下面的几条评论中提到,您尝试了
mysql\u real\u escape\u string
,但没有成功。请清楚地解释这是如何以及为什么不起作用的$this->db->insert('posts',$data)
@deep 3015的解决方案应该适合youPost内容可能包括特殊符号,因此我不想删除所有特殊符号,我只想删除或转义这些(“”)符号。@Rick是的,我得到了所有文字,直到我提到的符号为止。假设我有一个字符串-健身房围绕着这个独特的因素,称为“社区”。它是在这个社区内。像这样的东西,然后存储的数据将是这样的-健身房围绕着这个独特的因素,称为。当它得到符号,然后字符串从那个点截断并存储到数据库。实际上我有一个大规模上传表单。使用csv文件,我可以同时上传许多记录。css文件包含这些符号。@Deepak在读取csv文件时是否使用fgetcsv()
?如果这是utf8问题,也许您可以尝试:$data=fgetcsv(“file.csv”,“r”)代码>$data=array\u map(“utf8\u encode”,$data)代码>-数组映射将$data的每个值发送到utf8编码函数。在将值编码到utf8之后,您现在可以尝试一些建议,如str\u replace
或preg\u replace
您可以尝试将其添加为while语句的第一行:$importdata=array\u map(“utf8\u encode”,$importdata)
如果一切都设置为utf8,则不需要utf8\u编码
。
$html = base64_encode($this->input->post->('key'));