dongzhuo7291 2015-03-19 04:16 采纳率: 100%
浏览 21
已采纳

如何在PHP中截断分隔的字符串?

I have a string of delimited numerical values just like this:

5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004| ...etc.

Depending on the circumstance, the string may have only 1 value, 15 values, all the way up to 100s of values, all pipe delimited.

I need to count off (and keep/echo) the first 10 values and truncate everything else after that.

I've been looking at all the PHP string functions, but have been unsuccessful in finding a method to handle this directly.

  • 写回答

4条回答 默认 最新

  • doujie9882 2015-03-19 04:23
    关注

    Use explode() to separate the elements into an array, then you can slice off the first 10, and implode() them to create the new string.

    $arr = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
    $a = explode ('|',$arr);
    $b = array_slice($a,0,10);
    $c = implode('|', $b);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?