Feeds:
文章
留言

Archive for 六月 15th, 2009

解析 XML

$xml = ‘<?xml version="1.0" encoding="big5"?><xml><person><name age="30">cwchiu</name></person><person><name age="31">Chui-Wen Chiu</name></person></xml>’;

$aXml = new SimpleXmlIterator($xml);
foreach( $aXml->xpath(‘//person/name’) as $node){
    echo $node . ‘<br/>’;
}

foreach( $aXml->xpath(‘//person/name/@age’) as $node){
    echo $node . ‘<br/>’;
}

執行結果

image

Read Full Post »

可用來限制資料集合,也可應用成分頁資料。

$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$arrIter = new ArrayIterator($arr);
$offset = 5;
$total = 2;
$filterIter = new LimitIterator($arrIter, $offset, $total);
foreach($filterIter as $item){
    echo $item . ‘<br/>’;
}

執行結果會顯示 6 和 7

Read Full Post »

從 SplFileInfo 繼承,用於處理檔案資料。如下顯示文字檔案內容

file_put_contents(‘/tmp/test.txt’, file_get_contents(‘http://www.google.com’) );
foreach(new SplFileObject(‘/tmp/test.txt’) as $line){
    echo $line . ‘<br/>’;
}

執行結果

image

Read Full Post »

迭代資料過程中過濾 資料用。如下:

class MyFilter extends FilterIterator{
    function __construct($iter){
        parent::__construct($iter);
    }

    function accept(){
        return $this->current() % 2 == 0;
    }

}
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$arrIter = new ArrayIterator($arr);
$filterIter = new MyFilter($arrIter);

foreach($filterIter as $item){
    echo $item . ‘<br/>’;
}

執行結果

image

Read Full Post »

用來檢視指定目錄下的檔案和目錄集合。不過不支援迭代所有的子目錄,必須自己添加。

$folder = new DirectoryIterator(‘./webmeet/theme’);
// $item:SplFileInfo
foreach($folder as $item){
    if ($item->isDir()){
        echo ‘[‘ . $item->getFilename() . ‘]<br/>’;
    }else{
        echo $item->getFilename() . ‘<br/>’;
    }
}

image

Read Full Post »

用來回傳資料數量,以下範例是用 for 迭代實作 ArrayAccess + Countable 介面的物件。

class My implements ArrayAccess, Countable{
    private $name = ‘cwchiu’;
    private $sex = 1;
    private $age = 30;

    function offsetExists($offset){
        if ($offset >=0 && $offset <$this->count()){
            return true;
        }

        return false;
    }

    function offsetGet($offset){
        switch($offset){
        case 0:
            return $this->name;
        case 1:
            return $this->sex;
        case 2:
            return $this->age;
        }
    }

    function offsetSet($offset, $value){
        switch($offset){
        case 0:
            $this->name = $value;
        case 1:
            $this->age = $value;
        case 2:
            $this->age = $value;
        }
    }

    function offsetUnset($offset){
        switch($offset){
        case 0:
            unset( $this->name );
        case 1:
            unset( $this->sex );
        case 2:
            unset( $this->age );
        }
    }

    function count (){
        return 3;
    }

}

$aMy = new My();
for($i=0, $c=$aMy->count(); $i<$c; ++$i){
    echo $aMy[$i] . ‘<br/>’;
}

Read Full Post »

繼承 Iterator 介面,並新增 Seek( ) 用於快速移到指定位置。下面是一個簡單的範例

class My implements SeekableIterator{
    private $i=0;
    function rewind(){
        $this->i = 0;
    }

    function current(){
        return $this->i *5;
    }

    function key(){
        return $this->i;
    }

    function next(){
        ++$this->i;
    }

    function valid(){
        return $this->i<=5;
    }

    function seek($offset){
        if ($offset >5 || $offset < 0 ){
            throw new OutOfBoundsException(‘invalid pos’);
        }

        $this->i = $offset;
    }
}

$aMy = new My();
echo $aMy->current() . ‘<br/>’;
$aMy->seek(5);
echo $aMy->current() . ‘<br/>’;

執行結果:

image

Read Full Post »

Older Posts »