PHP怎么将数组的内容循环成 “链接”+“下拉选择”的样式
自己写的一个循环:
<?php $videolist = "优酷{span}youku{span}地址1{sl}地址2{sl}地址3{li}土豆{span}tudou{span}土豆地址{li}新浪{span}sina{span}新浪地址1{sl}新浪地址2{sl}新浪地址3{li}";$list = explode('{li}',$videolist); $palylist = ''; for($i=0;$i<count($list);$i++){ $video = explode('{span}',$list[$i]); if(empty($video[0])) continue ; if(empty($video[1])) continue ; $play = $i+1; //$url = $video[1]; $palylist .="<a href=\"&play={$play}\" target=\"_blank\">{$video[0]}</a> "; } echo $palylist;?>
<a href="&play=1" target="_blank">优酷</a> <a href="&play=2" target="_blank">土豆</a> <a href="&play=3" target="_blank">新浪</a>
<select name="youku"> <option>优酷</option> <option value="地址1">第一集</option> <option value="地址2">第二集</option> <option value="地址3">第三集</option> </select> <a href="土豆地址">土豆</a> <select name="sina"> <option>新浪</option> <option value="地址1">第一集</option> <option value="地址2">第二集</option> <option value="地址3">第三集</option> </select>
$videolist = "优酷{span}youku{span}地址1{sl}地址2{sl}地址3{li}土豆{span}tudou{span}土豆地址{li}新浪{span}sina{span}新浪地址1{sl}新浪地址2{sl}新浪地址3{li}";$ar=preg_split('/\{li\}/',$videolist,-1,PREG_SPLIT_NO_EMPTY); $html='';$j=array(0=>'第一集',1=>'第二集',2=>'第三集');foreach($ar as $v){ $arr=preg_split('/\{.*?\}/',$v) ; if(count($arr)>3) { $html.='<select name="'.$arr[1].'">'."\n" ; $html.='<option>'.$arr[0].'</option>'."\n"; foreach(array_slice($arr,2) as $kl=>$vl) $html.='<option value="'.$vl.'">'.$j[$kl].'</option>'."\n"; } else{ $html.='<a href="'.$arr[2].'">'.$arr[0].'</a>'."\n"; } }echo $html;
[解决办法]
睡不着觉,给你改一个吧,不是不会,估计你就是太懒。。。。。。。。。。
把数组中的{sl}换成{span}啦,要不还要取子串
<?php $videolist = "优酷{span}youku{span}地址1{span}地址2{span}地址3{li}土豆{span}tudou{span}土豆地址{li}新浪{span}sina{span}新浪地址1{span}新浪地址2{span}新浪地址3{li}";?><form name=123 action=""><?php$list = explode('{li}',$videolist); $palylist = ''; for($i=0;$i<count($list);$i++){ $video = explode('{span}',$list[$i]); //print_r($video); //foreach $video as $key=>$value if(!empty($video[0])) { echo"<select name=$video[1]>"; echo "<option>$video[0]</option>"; for($j=2;$j<count($video);$j++){ $m=$j-1; echo '<option value=地址'.$m.'>第'.change($m).'集</option>'; } echo "</select>" ; } //$url = $video[1]; } ?></form><?phpfunction change($number){//为了给你生成第几集,费老周折了 $number=substr($number,0,2); $arr=array("零","一","二","三","四","五","六","七","八","九"); if(strlen($number)==1){ $result=$arr[$number]; } else{ if($number==10){ $result="十"; }else{ if($number<20){ $result="十"; }else{ $result=$arr[substr($number,0,1)]."十"; } if(substr($number,1,1)!="0"){ $result.=$arr[substr($number,1,1)]; } } } return $result."<br>";}?>