日期:2014-05-17  浏览次数:20445 次

PHP怎么将数组的内容循环成 “链接”+“下拉选择”的样式
自己写的一个循环:
PHP code

<?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;

?>



以上循环出来的结果是:
HTML code

<a href="&play=1" target="_blank">优酷</a>  <a href="&play=2" target="_blank">土豆</a>  <a href="&play=3" target="_blank">新浪</a>



请问如何循环成如下样式:
HTML code

    <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>



------解决方案--------------------
PHP code
$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 code
<?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>



<?php