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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了