douchuang1861 2019-05-10 18:40
浏览 147
已采纳

使用contenteditable div的结构替换数据库中的值

I'm printing a value of a mysql field on a contenteditable div and I need it to adopt the structure corresponding to it. With the same divs and line breaks br.

I have tried the nl2br function of php but it only generates line breaks br, so it does not work well for me. I need the complete structure.

For example:

<div id="textbox" contenteditable="true"><?php echo nl2br($text); ?></div>

Where $text is a text with line breaks from database.

So when executing nl2br, change them all by br.

I currently get this:

<div id="textbox" contenteditable="true">
  Line 1
  <br>
  Line 2
  <br>
  <br>
  Line 3
</div>

And I need to get this:

<div id="textbox" contenteditable="true">
  Line 1
  <div>Line 2</div>
  <div>
    <br>
  </div>
  <div>Line 3</div>
</div>

Which is what is generated automatically when writing on a contenteditable div.

  • 写回答

1条回答 默认 最新

  • duanjiati1755 2019-05-10 19:20
    关注

    A quick attempt at what your after, output looks OK, but make sure it works for what your after.

    The idea is to just split the text into lines - start off with the first line of text as is, then add each subsequent line in <div> tags - if the line is blank - then set it to <br />...

    $text = 'Line 1
    Line 2
    
    Line 3';
    
    $lines = explode("
    ",$text);
    $output = array_shift($lines);
    foreach ( $lines as $line ) {
        $output .= "<div>".($line?:"<br />")."</div>";
    }
    
    echo $output;
    

    With the sample above this gives...

    Line 1<div>Line 2</div><div><br /></div><div>Line 3</div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊