代码之家  ›  专栏  ›  技术社区  ›  John Beasley

使用jQuery和PHP在项目符号点后插入换行符

  •  3
  • John Beasley  · 技术社区  · 7 年前

    我有一个用户在HTML文本区域中输入评论的页面最近,用户在评论中加入了要点。

    以下是用户如何在带有项目符号的HTML文本区域中输入注释:

    enter image description here

    现在,在保存注释时,下面是注释在数据库中的外观:

    enter image description here

    我在数据库中添加了其他列,以便清楚地看到有多个条目带有带项目符号的注释。

    返回数据,然后将注释显示到JQuery datatable单元格中时,注释的显示与下图中的显示完全相同:

    enter image description here

    如果您注意到图片,项目符号是段落形式的,而不是像最初在HTML文本区域中输入时那样分开。

    我需要做的是在datatable单元格中出现的每个项目符号点后面添加一个换行符,使其看起来像最初是如何输入的。

    我不确定是否需要对MySQL执行此操作,但是以下是查询数据库时PHP/MySQL的外观:

    <?php
    include("../include/database.php"); 
    
    $select = "SELECT pk, region, loctype, city, twenty, twenty_comm FROM eqpstatus";   
    $query = mysqli_query($dbc, $select);
    
    $out = array(); 
    while($row = $query->fetch_assoc())
    {
        $out[] = $row;
    }
    echo json_encode($out);
    ?>
    

    我不确定是否有方法检查MySQL查询中的要点。

    在jquery方面,以下是数据表返回到页面的方式(尽可能缩短):

    var newData = 'process/eqpGuide.php'; 
    
    $('#example1').DataTable({      
    "ajax": {
        "url": newData,
        "type": "POST",
        "dataSrc": ''
    },
    "columns": [
      {"data": "region"},
      {"data": "loctype"},
      {"data": "city"},
      {"data": "twenty"},
      {"data": "twenty_comm"} // here is where I need to check for bullet points
    ],
    "iDisplayLength": 25,
    "order": [[ 1, "desc" ]],
    "paging": false,
    "scrollY": 550,
    "scrollX": true,
    "bDestroy": true,
    "stateSave": true,
    "autoWidth": true 
    });   
    

    所以为了重申我所提到的一切,我需要做的一切都是关于数据库的20个comm部分,也就是在每个项目符号之后添加一个换行符。我不确定这是否应该或者可以在代码的php/mysql部分中完成,或者是否可以在datatable中的jquery端完成。

    在PHP/MySQL或jQuery中,如何在项目符号点后添加换行符?

    3 回复  |  直到 6 年前
        1
  •  2
  •   halfer    7 年前

    我想你是说你想加一个换行符 之前 子弹和后面的空白不可能知道 之后 是的,除了下一颗子弹之前你可以用专栏 render 回拨:

    { data: 'twenty_comm' ,
      render: function(data) {
         return data
           .replace(/•/g,'•&nbsp;&nbsp;')
           .replace(/(?!^)(•)/g, '<br>\$1')
      }
    }
    

    第一个regex只是用一个bullet和两个whitespaces替换bullet(或middot)接下来是一个负展望插入 <br> 在子弹之前除了第一次出现。

    http://jsfiddle.net/cj6nk8yz/

        2
  •  2
  •   Kisaragi    7 年前

    你也许可以试着用 preg_split() 和你的 twenty_comm 列并添加 <br /> char,不知道如何渲染:

    $row['twenty_comm'] = addNewLine($row['twenty_comm']); 
    
    function addNewLine($string){
       $newString = '';
       $split= preg_split("/[;,]+/",$string);
       foreach($split as $char){
            $newString.=$char.'<br />'; 
       }
          return $newString;
    }
    
        3
  •  2
  •   Louys Patrice Bessette    7 年前

    编辑

    我认为最好的做法是在用户提交文本时添加新行…在保存到数据库之前但这是一个观点。

    第二件最好的事情是在服务器端执行字符串操作,因为依赖于客户端的设备 (我删除的最不好的解决方案…) 在服务器上做可行的事情被认为是不好的做法。

    试试这个:

    <?php
    include("../include/database.php"); 
    
    $select = "SELECT pk, region, loctype, city, twenty, twenty_comm FROM eqpstatus";   
    $query = mysqli_query($dbc, $select);
    
    $out = array(); 
    while($row = $query->fetch_assoc())
    {
        $row['twenty_comm'] = implode("<br>•",explode("•",$row['twenty_comm']));
        if(substr($row['twenty_comm'],0,4)=="<br>"){
            $row['twenty_comm'] = substr($row['twenty_comm'],4);
        }
    
        $out[] = $row;
    }
    echo json_encode($out);
    ?>