douzhimei8259 2017-10-04 18:48
浏览 62
已采纳

仅在3个句子后添加<span>一次,然后在每3个句子后添加<p> </ p>

I have some long simple text and i need to insert <span id='colorme'></span> tag after first 5 sentences only once and after that i need to insert <p...></p> tags after every 5 sentences till the end of the text. But if full text less than 5 sentences, the script must do nothing.

For example:

Without false modesty, we state that we have the best staff possible. And it's not some kind of farce, fiction or someone's evil joke. No, no - this is the most sincere truth. All our employees are incredibly welcoming, smiling, polite, tidy and competent in their work. Thanks to this, our sauna has been working successfully for many years, bringing pleasure to all its customers, both permanent and new. Come, we will be glad to see you. With respect to you, Alina.

And i need:

Without false modesty, we state that we have the best staff possible. And it's not some kind of farce, fiction or someone's evil joke. No, no - this is the most sincere truth. <span id='colorme'></span> All our employees are incredibly welcoming, smiling, polite, tidy and competent in their work. Thanks to this, our sauna has been working successfully for many years, bringing pleasure to all its customers, both permanent and new. Come, we will be glad to see you. <p style='color:red'>www.example.com</p> With respect to you, Alina.

It's just an example. So have something like this, it's not work property, just added <span> after every 3 sentences but i need only once, i don't even know what must I do

<?php
$long_text = 'long long text';
$str = $long_text;
$arr = explode(".", $str);
$new_str = "";
$j = 1;
foreach($arr as $arr_el) {
    $new_str .= $arr_el.".";
    if($j % 3 == 0) {
        $new_str .= "<span id=colorme></span>";
    };
    $j++;
}

echo $new_str;?>
  • 写回答

1条回答 默认 最新

  • duanguan3863 2017-10-04 18:56
    关注

    change like below:-

    <?php
        $long_text = 'long long text';
        $str = $long_text;
        $arr = explode(".", $str);
        $new_str = "";
        $j = 1;
        foreach($arr as $arr_el) {
            $new_str .= $arr_el.".";
            if($j == 3) { // add span after first 3 sentences
                $new_str .= "<span id=colorme></span>";
            }else{
               if($j %3 == 0) { // now after each 3rd sentence add paragraph
                  $new_str .= "<p class=colorme></p>";
               }
            }
            $j++;
        }
        echo $new_str;
    ?>
    

    Note:- since <p></p> is going to repeat multiple time so i changed id to class, because multiple same id is not correct.

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

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换