【问题标题】:Storing content along with special characters in PHP在 PHP 中存储内容和特殊字符
【发布时间】:2017-03-10 06:39:43
【问题描述】:

我有一个add_post 表单,其中包括post_titlepost_contentdatepost_content 可能包含 special_characters 和 HTML 元素

存储到 post_content 有问题

示例字符串:

健身房围绕着这个称为“社区”的独特因素展开。

如果我尝试将该字符串插入到数据库表中,则会插入以下字符串:

健身房围绕着这个独特的因素,称为

由于符号()我无法插入所有数据

我只想删除这些符号,以便能够添加所有 post_content 数据

这是我尝试过的:

 $post_content=$_POST['post_content']; 
 $post_content=addslashes($post_content);
 $post_content=htmlentities($post_content);      

【问题讨论】:

  • 如何将数据插入数据库?您使用的是哪个数据库?您的数据库中的哪个字符集?您的问题不清楚,也没有显示出任何研究成果。
  • 您在下面的几个 cmets 中提到您尝试过mysql_real_escape_string,但没有成功。请清楚地解释这不起作用的原因和原因
  • $this->db->insert('posts', $data);
  • @deep 3015 的解决方案应该适合你

标签: php mysql codeigniter


【解决方案1】:

我只想删除这些符号,以便能够添加所有 post_content 数据。

回答这个问题(这与我的问题标题不同),您实际上可以删除特殊字符,然后再将其保存在控制器中的数据库中,它自己使用函数

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
}

您可以使用on line code tester 进行测试

<?php
$str = "'test 'this' “ ” ’ string'";
$str= str_replace(array("'", "\"","“","”","’"), "", $str );
$str=preg_replace("/\s+/", " ", $str);
echo $str;

结果将是test this string

【讨论】:

  • 帖子内容可能包含特殊符号,所以我不想删除所有特殊符号我只想删除或转义这些(“”)符号。
【解决方案2】:

如果你使用CodeIgniter,有相关问题herehere,你有两个选择:

使用查询构建:数据将被 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->e​​scape() 方法:显式转义数据

$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']) . "')");

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    utf8 问题。

    “ ” ’ 不是普通的 ascii 字符。如果它们被错误地馈送到CHARACTER SET utf8 列,则字符串可能会被截断。这就是正在发生的事情吗?你得到了所有的文本,但不包括那些引用字符之一?如果是这样,那么修复你的代码和/或架构的解释和提示是here——搜索“truncate”。

    可能的问题是输入的字节是用 latin1 编码的,而不是 utf8,但连接被指定为 UTF-8 并且表/列是列是 CHARACTER SET utf8(或 utf8mb4)。

    由于 PHP 或 MySQL 无法将这些字符识别为引号,因此许多其他建议无效。

    【讨论】:

    • @Rick 是的,我把所有文字都写到我提到的符号上。假设我有一个字符串——健身房围绕着这个称为“社区”的独特因素展开。它在这个社区内。像这样的东西,那么存储的数据就会像 - 健身房围绕着这个称为的独特因素。当它得到符号然后从该点截断字符串并存储到数据库中。
    • 其实我有一个海量上传表单。使用 csv 文件我可以同时上传许多记录。 css 文件包含这些符号。
    • @Deepak 你在阅读你的 csv 文件时使用fgetcsv() 吗?如果这是 utf8 问题,也许您可​​以尝试:$data = fgetcsv("file.csv", "r"); $data = array_map("utf8_encode", $data); - array_map 将 $data 的每个值发送到 utf8_encode 函数。在将值编码为 utf8 之后,也许您现在可以尝试类似 str_replacepreg_replace 的建议
    • 您可以尝试将其添加为您的 while 语句中的第一行:$importdata = array_map("utf8_encode", $importdata );
    • 如果 everything 设置为 utf8,则不需要utf8_encode
    【解决方案4】:

    你总是可以只base64_encode这些值,那么它们里面的内容并不重要。

    【讨论】:

      【解决方案5】:

      如果你只是想删除

      <?php
      $html="a“”’b";
      $res = preg_replace('/[^\x20-\x7E]/','', $html);
      echo $res;
      ?>
      

      输出ab

      【讨论】:

        【解决方案6】:

        我用一个技巧做了同样的事情

        $html = base64_encode($this->input->post->('key'));
        

        并在显示时对其进行解码!

        它将解决所有特殊字符问题

        【讨论】:

          【解决方案7】:

          使用mysql_real_escape_string

          举例

          $name='abc,cf';
           $name=mysql_real_escape_string($name);
          

          【讨论】:

            【解决方案8】:

            尝试使用表单助手插入数据并尝试这样set_value('description') 或使用此htmlspecialchars($POST['description']) 我使用此将数据保存到数据库中,并用于显示这样htmlspecialchars_decode($description)

            【讨论】:

              【解决方案9】:

              我不知道你为什么使用 mysqli_real_escape_string 或者为什么你需要将 "" 插入数据库,我使用 codeigniter 将文本插入我的数据库没有问题,traid 这个

              在控制器中

               $coments = $this->input->post('comentario', true); 
                  $query = $obj->Insertcoments($coments);
              

              在模型中

              function Insert($coments) {
                      $data = array(
                          'mensaje' => $coments);
                      $this->db->insert('mensaje', $data);
                  }
              

              【讨论】:

                猜你喜欢
                • 2022-06-23
                • 2023-03-26
                • 2020-12-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-06-14
                • 1970-01-01
                • 2011-04-21
                相关资源
                最近更新 更多