dqg63264 2015-01-18 02:09
浏览 33
已采纳

在while语句中的Wordwrap

Basically, I have a while statement and I want to make it so that if the text selected is too long, it will add a break every 20 characters or so.

I have made an attempt, but it isn't working. The main issue with this is on mobile if the text is more than 20 characters, it will go outside the divider its in.

My Whole While Statement:

while ($field = $result->fetch_assoc()) {
    if ($columnCount < 10) {
        $text = $field["text"];
        $newtext = wordwrap($text, 20, "<br />
");
        echo '
            <div class="well">
                <div class="row">
                    <div class="col-xs-10">
                        '.$newtext.'<br /><br /><a class="link" href="view.php?id='.$field["id"].'"><b>'.$field["name"].'</b> - '.time_elapsed_string($field["timestamp"]).'</a>
                    </div>
                    <div class="col-xs-2 text-center counter">
                        <form action="like.php?id='.$field["id"].'" method="POST" style="margin-bottom:0px;margin-top:-8%;">
                            <button class="btn btn-primary btn-sm" type="submit" name="submit" id="submit">&#9650</button><br />
                            <input type="hidden" name="id" value="'.$field["id"].'">
                        </form>
                        <div class="well well2 text-center" style="padding:0px;margin-bottom:0px;width:100% !important;">
                            '.$field["likes"].'
                        </div>
                        <form action="dislike.php?id='.$field["id"].'" method="POST">
                            <button class="btn btn-primary btn-sm" type="submit" name="submit" id="submit">&#9660;</button><br />
                            <input type="hidden" name="id" value="'.$field["id"].'">
                        </form>
                    </div>
                </div>
            </div>';
        $columnCount++;
    }
}

I made an attempt, but wasn't sure how to get it working exactly. There is no error here, it will appear with the text, but there won't be any break in the line after 20 characters or so.

Here is an image of one of the results that is being displayed with more than 20 characters and no break. If I view the source, there are no breaks either.

enter image description here

Edit: I would post an image of it on mobile, but I can't right now. Basically the text will just go completely outside the box and won't stay inside. It will go off the page and everything.

  • 写回答

1条回答 默认 最新

  • du2986 2015-01-18 02:19
    关注

    Try:

    $newtext = wordwrap($text, 20, "<br />
    ", true);
    

    The true is required to force the breaking of a long string no matter what.

    More details in the PHP docs for wordwrap.

    Alternatively you could try chunk_split

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助