时间:2020-10-09 22:41:27 | 栏目:PHP代码 | 点击:次
<?php //利用 explode 函数分割字符串到数组 $source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串 $hello = explode(',',$source); for($i=0;$i<count($hello);$i++){ echo $hello[$i];echo "</br>"; } ?>
截取去掉最后/第一个字符
$newstr = substr($str,0,strlen($str)-1);
取得数组的具体数据
$date = "04,30"; list($month, $day) = split ('[,.-]', $date); echo "Month: $month; Day: $day;<br />\n";
去除数组中重复的元素值 array_unique()
<meta charset="utf-8" /> <?php $a1="206,206,206,201,206,201"; //$array = explode(',', $a1); //字符串组成数组 $array1=implode(",",array_unique(explode(',', $a1))); print_r($array1); ?>
数组索引值如何重新从0开始递增
<?php $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse"); print_r(array_values($a)); // 输出: // Array ( [0] => Cat [1] => Dog [2] => Horse ) ?>
统计数组元素个数
$a="303,304,305,306,307"; $a = explode(',',$a); echo count($a);
JS split
<script language="javascript"> str="2,2,3,5,6,6"; //这是一字符串 var strs= new Array(); //定义一数组 strs=str.split(","); //字符分割 for (i=0;i<strs.length ;i++ ) { document.write(strs[i]+"<br/>"); //分割后的字符输出 } </script>
explode() 函数把字符串分割为数组。
// 示例 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2
implode() 函数把数组元素组合为一个字符串。
$array = array('a' => 1, 'b'=>2, 'c'=>3, 'd'=>4); $string = implode("-",$array) echo $string; //==== 结果就是:1-2-3-4;
总结