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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度