如何通过PHP解析文件中的点?

时间:2014-03-06 16:50:27

标签: php file

如何通过php解析文件中的点和变量赋值?

文件内容:

NAME1
NAME2
NAME3
name3.subname1
name3.subname2.othername1
name3.subname2.othername2
name3.subname2.othername1.anothername1
name3.subname2.othername2.anothername1
NAME4

我想做如下:

<ul>
  <li>name1</li>
  <li>name2</li>
  <li>name3
    <ul>
      <li>subname1</li>
      <li>subname2
        <ul>
          <li>othername1
            <ul>
              <li>anothername1</li>
            </ul>
          </li>
          <li>othername2
            <ul>
              <li>anothername1</li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

我尝试了以下方式,但我没有运行子查询

$projectsListIgnore = array ('.Drafts','.Sent','.Trash','.Junk','Spam');
$folders = imap_list($mbox, '{localhost:143/novalidate-cert}', '*');
foreach ($folders as $folder) 
{
   $folder = str_replace('{localhost:143/novalidate-cert}INBOX', '', $folder);
   if($folder != 'Trash' && $folder != 'Sent' && $folder != 'Drafts' && $folder != 'Junk')
   {
      $personelFolder1 = explode('.', $folder);
      if(count($personelFolder1)>1)
      {
         echo imapDecode($personelFolder1[1]).'<br />';
         echo '<br />';
      }else
      {
         echo imapDecode($personelFolder1[0]).'<br />';
         echo '<br />';
      }
   }
}

Imap或本地文件读取等$directory = '../../mail/localhost/username/subscriptions';

感谢。

1 个答案:

答案 0 :(得分:0)

我认为我的英语太差了:))

我尝试过这样做:

$mbox = imap_open('{localhost:143/novalidate-cert}', $userName, $password, OP_HALFOPEN);
$folders = imap_list($mbox, "{localhost:143/novalidate-cert}", "*");
foreach ($folders as $folder) {
    if($folder != '{localhost:143/novalidate-cert}INBOX.Trash' && $folder != '{localhost:143/novalidate-cert}INBOX.Sent' && $folder != '{localhost:143/novalidate-cert}INBOX.Drafts' && $folder != '{localhost:143/novalidate-cert}INBOX.Junk' && $folder != '{localhost:143/novalidate-cert}INBOX'){
       $points1 = substr_count($folder, '.');
       if($points1>0){
         $subFolder1 = $folder;
         $subFolder1 = strstr($subFolder1, '.');
         $subFolder1 = substr($subFolder1, 1);

         $points2 = substr_count($subFolder1, '.');
         if($points2>0){
            $subFolder2 = $subFolder1;
            $subFolder2 = strstr($subFolder2, '.');
            $subFolder2 = substr($subFolder2, 1);

            $points3 = substr_count($subFolder2, '.');
            if($points3>0){
                $subFolder3 = $subFolder2;
                $subFolder3 = strstr($subFolder3, '.');
                $subFolder3 = substr($subFolder3, 1);

                $points4 = substr_count($subFolder3, '.');
                if($points4>0){
                    $subFolder4 = $subFolder3;
                    $subFolder4 = strstr($subFolder4, '.');
                    $subFolder4 = substr($subFolder4, 1);

                    $points5 = substr_count($subFolder4, '.');
                    if($points5>0){
                        $subFolder5 = $subFolder4;
                        $subFolder5 = strstr($subFolder5, '.');
                        $subFolder5 = substr($subFolder5, 1);
                        //full folder name : $folder
                        echo $subFolder5;//I think five is enough
                    }else{
                        //full folder name : $folder
                        echo $subFolder4;
                    }

                }else{
                    //full folder name : $folder
                    echo $subFolder3;
                }

            }else{
                //full folder name : $folder
                echo $subFolder2;
            }

         }else{
            //full folder name : $folder
            echo $subFolder1;
         }
      }else{
        echo $folder;
      }
   }
}