Home  >  Article  >  Backend Development  >  处理字符串的好方法,该怎么解决

处理字符串的好方法,该怎么解决

WBOY
WBOYOriginal
2016-06-13 13:41:13903browse

处理字符串的好方法
传来的参数格式为下划线分隔的数字,字串长度随机。比如:
1_1
1_2_10
12_1_5_10...
等等。
拿1_2_10为例,要对它处理得到1、1_2、1_2_10;
拿12_1_5_10为例,要对它处理得到12、12_1、12_1_5、12_1_5_10 (规律看出来了吧)
求好的算法。


------解决方案--------------------

PHP code
$s = '12_1_5_10';

$r[] = strtok($s, '_');
for($i=0; $t = strtok('_'); $i++) {
  $r[] = $r[$i] . '_' . $t;
}
print_r($r);
<br><font color="#e78608">------解决方案--------------------</font><br>还是老大猛,我的就是裹脚布<br><br>$str = '12_1_5_10';<br>$array = explode('_', $str);<br>$newarr = array();<br>for ($i = 0; $i    $newarr[]=array_slice($array,0,$i+1);<br>}<br>foreach($newarr as $value){<br>   echo '<pre class="brush:php;toolbar:false">'.implode('_',$value).'
';
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn