doushan7077 2013-07-25 09:09
浏览 15
已采纳

把所有东西变成“......”之后; 这是句子中的第100个位置

Here I have a string: "status; status;" and so on... I need to cut the string from the status where string is longer than 100 chars

for example: "status; status; status; (here is longer than 100 chars) ..."

previously I did it with the array:

 if($length_of_string > 100)
 { 
     $number_of_elements = count($statuses_array);
     echo $statuses_array[$number_of_elements-1];
     echo ' ... ';
     } else {
     echo $string_of_statuses;
}

but it is not good

thank you in advance!

  • 写回答

3条回答 默认 最新

  • dso15221 2013-07-25 09:29
    关注

    How about:

    $l = strlen($status_string);
    if ($l > 100) {
       //Split string into array of statuses, to not break last status
       $ls = explode(";",$status_string);
       $new_status = "";
       $i = 0;
       //Check if there is room for the next status in $new_status without passing 100 chars
       while (strlen($new_status) < 100) {
          $new_status .= $ls[$i].";";
          $i++;
       }
       $new_status = substr($new_status,0,-1)."...";
    }
    

    Edit: simplified code, as the last status apparently can surpass 100 chars, which actually makes it easier

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?