当前位置: 首页 > 编程日记 > 正文

PHP SPL笔记

PHP SPL笔记

作者: 阮一峰

日期: 2008年7月 8日

这几天,我在学习PHP语言中的SPL。

这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记。不然记不住,以后要用的时候,还是要从头学起。

由于这是供自己参考的笔记,不是教程,所以写得比较简单,没有多解释。但是我想,如果你是一个熟练的PHP5程序员,应该足以看懂下面的材料,而且会发现它很有用。现在除此之外,网上根本没有任何深入的SPL中文介绍。

================

PHP SPL笔记

目录

第一部分 简介

1. 什么是SPL?

2. 什么是Iterator?

第二部分 SPL Interfaces

3. Iterator界面

4. ArrayAccess界面

5. IteratorAggregate界面

6. RecursiveIterator界面

7. SeekableIterator界面

8. Countable界面

第三部分 SPL Classes

9. SPL的内置类

10. DirectoryIterator类

11. ArrayObject类

12. ArrayIterator类

13. RecursiveArrayIterator类和RecursiveIteratorIterator类

14. FilterIterator类

15. SimpleXMLIterator类

16. CachingIterator类

17. LimitIterator类

18. SplFileObject类

第一部 简介

1. 什么是SPL?

SPL是Standard PHP Library(PHP标准库)的缩写。

根据官方定义,它是“a collection of interfaces and classes that are meant to solve standard problems”。但是,目前在使用中,SPL更多地被看作是一种使object(物体)模仿array(数组)行为的interfaces和classes。

2. 什么是Iterator?

SPL的核心概念就是Iterator。这指的是一种Design Pattern,根据《Design Patterns》一书的定义,Iterator的作用是“provide an object which traverses some aggregate structure, abstracting away assumptions about the implementation of that structure.”

wikipedia中说,"an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation".……"the iterator pattern is a design pattern in which iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation".

通俗地说,Iterator能够使许多不同的数据结构,都能有统一的操作界面,比如一个数据库的结果集、同一个目录中的文件集、或者一个文本中每一行构成的集合。

如果按照普通情况,遍历一个MySQL的结果集,程序需要这样写:

// Fetch the "aggregate structure"
$result = mysql_query("SELECT * FROM users");
// Iterate over the structure
while ( $row = mysql_fetch_array($result) ) {
// do stuff with the row here
}

读出一个目录中的内容,需要这样写:

// Fetch the "aggregate structure"
$dh = opendir('/home/harryf/files');
// Iterate over the structure
while ( $file = readdir($dh) ) {
// do stuff with the file here
}

读出一个文本文件的内容,需要这样写:

// Fetch the "aggregate structure"
$fh = fopen("/home/hfuecks/files/results.txt", "r");
// Iterate over the structure
while (!feof($fh)) {
$line = fgets($fh);
// do stuff with the line here
}

上面三段代码,虽然处理的是不同的resource(资源),但是功能都是遍历结果集(loop over contents),因此Iterator的基本思想,就是将这三种不同的操作统一起来,用同样的命令界面,处理不同的资源。

第二部分 SPL Interfaces

3. Iterator界面

SPL规定,所有部署了Iterator界面的class,都可以用在foreach Loop中。Iterator界面中包含5个必须部署的方法:

    * current()
This method returns the current index’s value. You are solely
responsible for tracking what the current index is as the 
interface does not do this for you.
* key()
This method returns the value of the current index’s key. For 
foreach loops this is extremely important so that the key 
value can be populated.
* next()
This method moves the internal index forward one entry.
* rewind()
This method should reset the internal index to the first element.
* valid()
This method should return true or false if there is a current 
element. It is called after rewind() or next().

下面就是一个部署了Iterator界面的class示例:

/**
* An iterator for native PHP arrays, re-inventing the wheel
*
* Notice the "implements Iterator" - important!
*/
class ArrayReloaded implements Iterator {
/**
* A native PHP array to iterate over
*/
private $array = array();
/**
* A switch to keep track of the end of the array
*/
private $valid = FALSE;
/**
* Constructor
* @param array native PHP array to iterate over
*/
function __construct($array) {
$this->array = $array;
}
/**
* Return the array "pointer" to the first element
* PHP's reset() returns false if the array has no elements
*/
function rewind(){
$this->valid = (FALSE !== reset($this->array));
}
/**
* Return the current array element
*/
function current(){
return current($this->array);
}
/**
* Return the key of the current array element
*/
function key(){
return key($this->array);
}
/**
* Move forward by one
* PHP's next() returns false if there are no more elements
*/
function next(){
$this->valid = (FALSE !== next($this->array));
}
/**
* Is the current element valid?
*/
function valid(){
return $this->valid;
}
}

使用方法如下:

// Create iterator object
$colors = new ArrayReloaded(array ('red','green','blue',));
// Iterate away!
foreach ( $colors as $color ) {
echo $color."<br>";
}

你也可以在foreach循环中使用key()方法:

// Display the keys as well
foreach ( $colors as $key => $color ) {
echo "$key: $color<br>";
}

除了foreach循环外,也可以使用while循环,

// Reset the iterator - foreach does this automatically
$colors->rewind();
// Loop while valid
while ( $colors->valid() ) {
echo $colors->key().": ".$colors->current()."
";
$colors->next();
}

根据测试,while循环要稍快于foreach循环,因为运行时少了一层中间调用。

4. ArrayAccess界面

部署ArrayAccess界面,可以使得object像array那样操作。ArrayAccess界面包含四个必须部署的方法:

    * offsetExists($offset)
This method is used to tell php if there is a value
for the key specified by offset. It should return 
true or false.
* offsetGet($offset)
This method is used to return the value specified 
by the key offset.
* offsetSet($offset, $value)
This method is used to set a value within the object, 
you can throw an exception from this function for a 
read-only collection.
* offsetUnset($offset)
This method is used when a value is removed from 
an array either through unset() or assigning the key 
a value of null. In the case of numerical arrays, this 
offset should not be deleted and the array should 
not be reindexed unless that is specifically the 
behavior you want.

下面就是一个部署ArrayAccess界面的实例:

/**
* A class that can be used like an array
*/
class Article implements ArrayAccess {
public $title;
public $author;
public $category;  
function __construct($title,$author,$category) {
$this->title = $title;
$this->author = $author;
$this->category = $category;
}
/**
* Defined by ArrayAccess interface
* Set a value given it's key e.g. $A['title'] = 'foo';
* @param mixed key (string or integer)
* @param mixed value
* @return void
*/
function offsetSet($key, $value) {
if ( array_key_exists($key,get_object_vars($this)) ) {
$this->{$key} = $value;
}
}
/**
* Defined by ArrayAccess interface
* Return a value given it's key e.g. echo $A['title'];
* @param mixed key (string or integer)
* @return mixed value
*/
function offsetGet($key) {
if ( array_key_exists($key,get_object_vars($this)) ) {
return $this->{$key};
}
}
/**
* Defined by ArrayAccess interface
* Unset a value by it's key e.g. unset($A['title']);
* @param mixed key (string or integer)
* @return void
*/
function offsetUnset($key) {
if ( array_key_exists($key,get_object_vars($this)) ) {
unset($this->{$key});
}
}
/**
* Defined by ArrayAccess interface
* Check value exists, given it's key e.g. isset($A['title'])
* @param mixed key (string or integer)
* @return boolean
*/
function offsetExists($offset) {
return array_key_exists($offset,get_object_vars($this));
}
}

使用方法如下:

// Create the object
$A = new Article('SPL Rocks','Joe Bloggs', 'PHP');
// Check what it looks like
echo 'Initial State:<div>';
print_r($A);
echo '</div>';
// Change the title using array syntax
$A['title'] = 'SPL _really_ rocks';
// Try setting a non existent property (ignored)
$A['not found'] = 1;
// Unset the author field
unset($A['author']);
// Check what it looks like again
echo 'Final State:<div>';
print_r($A);
echo '</div>';

运行结果如下:

Initial State:
Article Object
(
[title] => SPL Rocks
[author] => Joe Bloggs
[category] => PHP
)
Final State:
Article Object
(
[title] => SPL _really_ rocks
[category] => PHP
)

可以看到,$A虽然是一个object,但是完全可以像array那样操作。

你还可以在读取数据时,增加程序内部的逻辑:

function offsetGet($key) {
if ( array_key_exists($key,get_object_vars($this)) ) {
return strtolower($this->{$key});
}
}

5. IteratorAggregate界面

但是,虽然$A可以像数组那样操作,却无法使用foreach遍历,除非部署了前面提到的Iterator界面。

另一个解决方法是,有时会需要将数据和遍历部分分开,这时就可以部署IteratorAggregate界面。它规定了一个getIterator()方法,返回一个使用Iterator界面的object。

还是以上一节的Article类为例:

class Article implements ArrayAccess, IteratorAggregate {
/**
* Defined by IteratorAggregate interface
* Returns an iterator for for this object, for use with foreach
* @return ArrayIterator
*/
function getIterator() {
return new ArrayIterator($this);
}

使用方法如下:

$A = new Article('SPL Rocks','Joe Bloggs', 'PHP');
// Loop (getIterator will be called automatically)
echo 'Looping with foreach:<div>';
foreach ( $A as $field => $value ) {
echo "$field : $value<br>";
}
echo '</div>';
// Get the size of the iterator (see how many properties are left)
echo "Object has ".sizeof($A->getIterator())." elements";

显示结果如下:

Looping with foreach:
title : SPL Rocks
author : Joe Bloggs
category : PHP
Object has 3 elements

6. RecursiveIterator界面

这个界面用于遍历多层数据,它继承了Iterator界面,因而也具有标准的current()、key()、next()、 rewind()和valid()方法。同时,它自己还规定了getChildren()和hasChildren()方法。The getChildren() method must return an object that implements RecursiveIterator.

7. SeekableIterator界面

SeekableIterator界面也是Iterator界面的延伸,除了Iterator的5个方法以外,还规定了seek()方法,参数是元素的位置,返回该元素。如果该位置不存在,则抛出OutOfBoundsException。

下面是一个是实例:

<?php
class PartyMemberIterator implements SeekableIterator
{
public function __construct(PartyMember $member)
{
// Store $member locally for iteration
}
public function seek($index)
{
$this->rewind();
$position = 0;
while ($position < $index && $this->valid()) {
$this->next();
$position++;
}
if (!$this->valid()) {
throw new OutOfBoundsException('Invalid position');
}
}
// Implement current(), key(), next(), rewind()
// and valid() to iterate over data in $member
}
?>

8. Countable界面

这个界面规定了一个count()方法,返回结果集的数量。

第三部分 SPL Classes

9. SPL的内置类

SPL除了定义一系列Interfaces以外,还提供一系列的内置类,它们对应不同的任务,大大简化了编程。

查看所有的内置类,可以使用下面的代码:

<?php
// a simple foreach() to traverse the SPL class names
foreach(spl_classes() as $key=>$value)
{
echo $key.' -&gt; '.$value.'<br />';
}
?>

10. DirectoryIterator类

这个类用来查看一个目录中的所有文件和子目录:

<?php
try{
/*** class create new DirectoryIterator Object ***/
foreach ( new DirectoryIterator('./') as $Item )
{
echo $Item.'<br />';
}
}
/*** if an exception is thrown, catch it here ***/
catch(Exception $e){
echo 'No files Found!<br />';
}
?>

查看文件的详细信息:

<table>
<?php
foreach(new DirectoryIterator('./' ) as $file )
{
if( $file->getFilename()  == 'foo.txt' )
{
echo '<tr><td>getFilename()</td><td> '; var_dump($file->getFilename()); echo '</td></tr>';
echo '<tr><td>getBasename()</td><td> '; var_dump($file->getBasename()); echo '</td></tr>';
echo '<tr><td>isDot()</td><td> '; var_dump($file->isDot()); echo '</td></tr>';
echo '<tr><td>__toString()</td><td> '; var_dump($file->__toString()); echo '</td></tr>';
echo '<tr><td>getPath()</td><td> '; var_dump($file->getPath()); echo '</td></tr>';
echo '<tr><td>getPathname()</td><td> '; var_dump($file->getPathname()); echo '</td></tr>';
echo '<tr><td>getPerms()</td><td> '; var_dump($file->getPerms()); echo '</td></tr>';
echo '<tr><td>getInode()</td><td> '; var_dump($file->getInode()); echo '</td></tr>';
echo '<tr><td>getSize()</td><td> '; var_dump($file->getSize()); echo '</td></tr>';
echo '<tr><td>getOwner()</td><td> '; var_dump($file->getOwner()); echo '</td></tr>';
echo '<tr><td>$file->getGroup()</td><td> '; var_dump($file->getGroup()); echo '</td></tr>';
echo '<tr><td>getATime()</td><td> '; var_dump($file->getATime()); echo '</td></tr>';
echo '<tr><td>getMTime()</td><td> '; var_dump($file->getMTime()); echo '</td></tr>';
echo '<tr><td>getCTime()</td><td> '; var_dump($file->getCTime()); echo '</td></tr>';
echo '<tr><td>getType()</td><td> '; var_dump($file->getType()); echo '</td></tr>';
echo '<tr><td>isWritable()</td><td> '; var_dump($file->isWritable()); echo '</td></tr>';
echo '<tr><td>isReadable()</td><td> '; var_dump($file->isReadable()); echo '</td></tr>';
echo '<tr><td>isExecutable(</td><td> '; var_dump($file->isExecutable()); echo '</td></tr>';
echo '<tr><td>isFile()</td><td> '; var_dump($file->isFile()); echo '</td></tr>';
echo '<tr><td>isDir()</td><td> '; var_dump($file->isDir()); echo '</td></tr>';
echo '<tr><td>isLink()</td><td> '; var_dump($file->isLink()); echo '</td></tr>';
echo '<tr><td>getFileInfo()</td><td> '; var_dump($file->getFileInfo()); echo '</td></tr>';
echo '<tr><td>getPathInfo()</td><td> '; var_dump($file->getPathInfo()); echo '</td></tr>';
echo '<tr><td>openFile()</td><td> '; var_dump($file->openFile()); echo '</td></tr>';
echo '<tr><td>setFileClass()</td><td> '; var_dump($file->setFileClass()); echo '</td></tr>';
echo '<tr><td>setInfoClass()</td><td> '; var_dump($file->setInfoClass()); echo '</td></tr>';
}
}
?>
</table>

除了foreach循环外,还可以使用while循环:

<?php
/*** create a new iterator object ***/
$it = new DirectoryIterator('./');
/*** loop directly over the object ***/
while($it->valid())
{
echo $it->key().' -- '.$it->current().'<br />';
/*** move to the next iteration ***/
$it->next();
}
?>

如果要过滤所有子目录,可以在valid()方法中过滤:

<?php
/*** create a new iterator object ***/
$it = new DirectoryIterator('./');
/*** loop directly over the object ***/
while($it->valid())
{
/*** check if value is a directory ***/
if($it->isDir())
{
/*** echo the key and current value ***/
echo $it->key().' -- '.$it->current().'<br />';
}
/*** move to the next iteration ***/
$it->next();
}
?>

11. ArrayObject类

这个类可以将Array转化为object。

<?php
/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
/*** create the array object ***/
$arrayObj = new ArrayObject($array);
/*** iterate over the array ***/
for($iterator = $arrayObj->getIterator();
/*** check if valid ***/
$iterator->valid();
/*** move to the next array member ***/
$iterator->next())
{
/*** output the key and current array value ***/
echo $iterator->key() . ' => ' . $iterator->current() . '<br />';
}
?>

增加一个元素:

$arrayObj->append('dingo');

对元素排序:

$arrayObj->natcasesort();

显示元素的数量:

echo $arrayObj->count();

删除一个元素:

$arrayObj->offsetUnset(5);

某一个元素是否存在:

 if ($arrayObj->offsetExists(3))
{
echo 'Offset Exists<br />';
}

更改某个位置的元素值:

 $arrayObj->offsetSet(5, "galah");

显示某个位置的元素值:

echo $arrayObj->offsetGet(4);
12. ArrayIterator类

这个类实际上是对ArrayObject类的补充,为后者提供遍历功能。

示例如下:

<?php
/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
try {
$object = new ArrayIterator($array);
foreach($object as $key=>$value)
{
echo $key.' => '.$value.'<br />';
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>

ArrayIterator类也支持offset类方法和count()方法:

<ul>
<?php
/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
try {
$object = new ArrayIterator($array);
/*** check for the existence of the offset 2 ***/
if($object->offSetExists(2))
{
/*** set the offset of 2 to a new value ***/
$object->offSetSet(2, 'Goanna');
}
/*** unset the kiwi ***/
foreach($object as $key=>$value)
{
/*** check the value of the key ***/
if($object->offSetGet($key) === 'kiwi')
{
/*** unset the current key ***/
$object->offSetUnset($key);
}
echo '<li>'.$key.' - '.$value.'</li>'."\n";
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>
</ul>

13. RecursiveArrayIterator类和RecursiveIteratorIterator类

ArrayIterator类和ArrayObject类,只支持遍历一维数组。如果要遍历多维数组,必须先用RecursiveIteratorIterator生成一个Iterator,然后再对这个Iterator使用RecursiveIteratorIterator。

<?php
$array = array(
array('name'=>'butch', 'sex'=>'m', 'breed'=>'boxer'),
array('name'=>'fido', 'sex'=>'m', 'breed'=>'doberman'),
array('name'=>'girly','sex'=>'f', 'breed'=>'poodle')
);
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key=>$value)
{
echo $key.' -- '.$value.'<br />';
}
?>

14. FilterIterator类

FilterIterator类可以对元素进行过滤,只要在accept()方法中设置过滤条件就可以了。

示例如下:

<?php
/*** a simple array ***/
$animals = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'NZ'=>'kiwi', 'kookaburra', 'platypus');
class CullingIterator extends FilterIterator{
/*** The filteriterator takes  a iterator as param: ***/
public function __construct( Iterator $it ){
parent::__construct( $it );
}
/*** check if key is numeric ***/
function accept(){
return is_numeric($this->key());
}
}/*** end of class ***/
$cull = new CullingIterator(new ArrayIterator($animals));
foreach($cull as $key=>$value)
{
echo $key.' == '.$value.'<br />';
}
?>

下面是另一个返回质数的例子:

<?php
class PrimeFilter extends FilterIterator{
/*** The filteriterator takes  a iterator as param: ***/
public function __construct(Iterator $it){
parent::__construct($it);
}
/*** check if current value is prime ***/
function accept(){
if($this->current() % 2 != 1)
{
return false;
}
$d = 3;
$x = sqrt($this->current());
while ($this->current() % $d != 0 && $d < $x)
{
$d += 2;
}
return (($this->current() % $d == 0 && $this->current() != $d) * 1) == 0 ? true : false;
}
}/*** end of class ***/
/*** an array of numbers ***/
$numbers = range(212345,212456);
/*** create a new FilterIterator object ***/
$primes = new primeFilter(new ArrayIterator($numbers));
foreach($primes as $value)
{
echo $value.' is prime.<br />';
}
?>

15. SimpleXMLIterator类

这个类用来遍历xml文件。

示例如下:

<?php
/*** a simple xml tree ***/
$xmlstring = <<<XML
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<document>
<animal>
<category id="26">
<species>Phascolarctidae</species>
<type>koala</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="27">
<species>macropod</species>
<type>kangaroo</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="28">
<species>diprotodon</species>
<type>wombat</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="31">
<species>macropod</species>
<type>wallaby</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="21">
<species>dromaius</species>
<type>emu</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="22">
<species>Apteryx</species>
<type>kiwi</type>
<name>Troy</name>
</category>
</animal>
<animal>
<category id="23">
<species>kingfisher</species>
<type>kookaburra</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="48">
<species>monotremes</species>
<type>platypus</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="4">
<species>arachnid</species>
<type>funnel web</type>
<name>Bruce</name>
<legs>8</legs>
</category>
</animal>
</document>
XML;
/*** a new simpleXML iterator object ***/
try    {
/*** a new simple xml iterator ***/
$it = new SimpleXMLIterator($xmlstring);
/*** a new limitIterator object ***/
foreach(new RecursiveIteratorIterator($it,1) as $name => $data)
{
echo $name.' -- '.$data.'<br />';
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

new RecursiveIteratorIterator($it,1)表示显示所有包括父元素在内的子元素。

显示某一个特定的元素值,可以这样写:

<?php
try {
/*** a new simpleXML iterator object ***/
$sxi =  new SimpleXMLIterator($xmlstring);
foreach ( $sxi as $node )
{
foreach($node as $k=>$v)
{
echo $v->species.'<br />';
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

相对应的while循环写法为:

<?php
try {
$sxe = simplexml_load_string($xmlstring, 'SimpleXMLIterator');
for ($sxe->rewind(); $sxe->valid(); $sxe->next())
{
if($sxe->hasChildren())
{
foreach($sxe->getChildren() as $element=>$value)
{
echo $value->species.'<br />';
}
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

最方便的写法,还是使用xpath:

<?php
try {
/*** a new simpleXML iterator object ***/
$sxi =  new SimpleXMLIterator($xmlstring);
/*** set the xpath ***/
$foo = $sxi->xpath('animal/category/species');
/*** iterate over the xpath ***/
foreach ($foo as $k=>$v)
{
echo $v.'<br />';
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

下面的例子,显示有namespace的情况:

<?php
/*** a simple xml tree ***/
$xmlstring = <<<XML
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<document xmlns:spec="http://example.org/animal-species">
<animal>
<category id="26">
<species>Phascolarctidae</species>
<spec:name>Speed Hump</spec:name>
<type>koala</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="27">
<species>macropod</species>
<spec:name>Boonga</spec:name>
<type>kangaroo</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="28">
<species>diprotodon</species>
<spec:name>pot holer</spec:name>
<type>wombat</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="31">
<species>macropod</species>
<spec:name>Target</spec:name>
<type>wallaby</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="21">
<species>dromaius</species>
<spec:name>Road Runner</spec:name>
<type>emu</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="22">
<species>Apteryx</species>
<spec:name>Football</spec:name>
<type>kiwi</type>
<name>Troy</name>
</category>
</animal>
<animal>
<category id="23">
<species>kingfisher</species>
<spec:name>snaker</spec:name>
<type>kookaburra</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="48">
<species>monotremes</species>
<spec:name>Swamp Rat</spec:name>
<type>platypus</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="4">
<species>arachnid</species>
<spec:name>Killer</spec:name>
<type>funnel web</type>
<name>Bruce</name>
<legs>8</legs>
</category>
</animal>
</document>
XML;
/*** a new simpleXML iterator object ***/
try {
/*** a new simpleXML iterator object ***/
$sxi =  new SimpleXMLIterator($xmlstring);
$sxi-> registerXPathNamespace('spec', 'http://www.exampe.org/species-title');
/*** set the xpath ***/
$result = $sxi->xpath('//spec:name');
/*** get all declared namespaces ***/
foreach($sxi->getDocNamespaces('animal') as $ns)
{
echo $ns.'<br />';
}
/*** iterate over the xpath ***/
foreach ($result as $k=>$v)
{
echo $v.'<br />';
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

增加一个节点:

<?php 
$xmlstring = <<<XML
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<document>
<animal>koala</animal>
<animal>kangaroo</animal>
<animal>wombat</animal>
<animal>wallaby</animal>
<animal>emu</animal>
<animal>kiwi</animal>
<animal>kookaburra</animal>
<animal>platypus</animal>
<animal>funnel web</animal>
</document>
XML;
try {
/*** a new simpleXML iterator object ***/
$sxi =  new SimpleXMLIterator($xmlstring);
/*** add a child ***/
$sxi->addChild('animal', 'Tiger');
/*** a new simpleXML iterator object ***/
$new = new SimpleXmlIterator($sxi->saveXML());
/*** iterate over the new tree ***/
foreach($new as $val)
{
echo $val.'<br />';
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

增加属性:

<?php 
$xmlstring =<<<XML
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<document>
<animal>koala</animal>
<animal>kangaroo</animal>
<animal>wombat</animal>
<animal>wallaby</animal>
<animal>emu</animal>
<animal>kiwi</animal>
<animal>kookaburra</animal>
<animal>platypus</animal>
<animal>funnel web</animal>
</document>
XML;
try {
/*** a new simpleXML iterator object ***/
$sxi =  new SimpleXMLIterator($xmlstring);
/*** add an attribute with a namespace ***/
$sxi->addAttribute('id:att1', 'good things', 'urn::test-foo');
/*** add an attribute without a  namespace ***/
$sxi->addAttribute('att2', 'no-ns');
echo htmlentities($sxi->saveXML());
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

16. CachingIterator类

这个类有一个hasNext()方法,用来判断是否还有下一个元素。

示例如下:

<?php
/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
try {
/*** create a new object ***/
$object = new CachingIterator(new ArrayIterator($array));
foreach($object as $value)
{
echo $value;
if($object->hasNext())
{
echo ',';
}
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>

17. LimitIterator类

这个类用来限定返回结果集的数量和位置,必须提供offset和limit两个参数,与SQL命令中limit语句类似。

示例如下:

<?php
/*** the offset value ***/
$offset = 3;
/*** the limit of records to show ***/
$limit = 2;
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
$it = new LimitIterator(new ArrayIterator($array), $offset, $limit);
foreach($it as $k=>$v)
{
echo $it->getPosition().'<br />';
}
?>

另一个例子是:

<?php
/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
$it = new LimitIterator(new ArrayIterator($array));
try
{
$it->seek(5);
echo $it->current();
}
catch(OutOfBoundsException $e)
{
echo $e->getMessage() . "<br />";
}
?>

18. SplFileObject类

这个类用来对文本文件进行遍历。

示例如下:

<?php
try{
// iterate directly over the object
foreach( new SplFileObject(&quot;/usr/local/apache/logs/access_log&quot;) as $line)
// and echo each line of the file
echo $line.'<br />';
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>

返回文本文件的第三行,可以这样写:

<?php
try{
$file = new SplFileObject("/usr/local/apache/logs/access_log");
$file->seek(3);
echo $file->current();
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>

相关文章:

算力超越 iPhone,芯片堪比Mac,网友:“买来能干啥?”

整理 | 郑丽媛出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;自去年“元宇宙”概念突然爆火&#xff0c;作为其“入门钥匙”的 AR/VR 设备也顺势成为了话题焦点&#xff0c;尤其在多家外媒爆料苹果也在为此发力、甚至从 Meta 挖人以争取在 2022 年正式推出时&…

ios开发日记- 5 屏幕截图

-(void)fullScreenshots{UIWindow *screenWindow [[UIApplication sharedApplication] keyWindow]; UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图&#xff0c;包括window [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage …

MaxCompute助力OSS支持EB级计算力

一、 MaxCompute是什么&#xff1f; 你的OSS数据是否作堆积在一旁沉睡已久&#xff0c;存储成本变为企业负担&#xff1f;你是否想唤醒沉睡的数据&#xff0c;驱动你的业务前行&#xff1f;MaxCompute可以帮助你高效且低成本的解决这些问题&#xff0c;通过对海量数据进行分析和…

php自动加载

很多开发者写面向对象的应用程序时对每个类的定义建立一个 PHP 源文件。一个很大的烦恼是不得不在每个脚本&#xff08;每个类一个文件&#xff09;开头写一个长长的包含文件列表。 在 PHP 5 中&#xff0c;不再需要这样了。可以定义一个 __autoload 函数&#xff0c;它会在试…

22个案例详解 Pandas 数据分析/预处理时的实用技巧,超简单

作者 | 俊欣来源 | 关于数据分析与可视化今天小编打算来讲一讲数据分析方面的内容&#xff0c;整理和总结一下Pandas在数据预处理和数据分析方面的硬核干货&#xff0c;我们大致会说Pandas计算交叉列表Pandas将字符串与数值转化成时间类型Pandas将字符串转化成数值类型Pandas当…

《mysql性能调优与架构设计》笔记: 一mysql 架构组成

2019独角兽企业重金招聘Python工程师标准>>> 2.1mysql物理文件组成 2.1.1日志文件&#xff1a; 1&#xff0c;查看mysql配置文件&#xff1a;mysql --verbose --help | grep -A 1 Default options; 1&#xff0c;错误日志&#xff1a;--log-error[file_name] 指定错…

发现一个可以搜索常用rpm包的地址(http://www.rpmfind.net/)

http://www.rpmfind.net/ 虽然资源不多&#xff0c;但也够用。 >如有问题&#xff0c;请联系我&#xff1a;easonjim#163.com&#xff0c;或者下方发表评论。<

PHP版UTF-8文件BOM自动检测移除程序

BOM信息是文件开头的一串隐藏的字符&#xff0c;用于让某些编辑器识别这是个UTF-8编码的文件。但PHP在读取文件时会把这些字符读出&#xff0c;从而形成了文件 开头含有一些无法识别的字符的问题。比如用UTF-8格式保存的生成图片的PHP文件&#xff0c;因为文件头隐藏的BOM信息也…

java: web应用中不经意的内存泄露

前面有一篇讲解如何在spring mvc web应用中一启动就执行某些逻辑&#xff0c;今天无意发现如果使用不当&#xff0c;很容易引起内存泄露&#xff0c;测试代码如下&#xff1a; 1、定义一个类App package com.cnblogs.yjmyzz.web.controller;import java.util.Date;public class…

「游戏圈地震级消息」687亿美元,微软收购游戏巨头动视暴雪

整理 | 苏宓、禾木木 出品 | CSDN 2022年1月18日晚&#xff0c;一条热搜刷爆了朋友圈&#xff1a; 继 2018 年&#xff0c;微软以 75 亿美元收购全球知名的代码托管平台 GitHub 后&#xff0c;2022 年 1 月 18 日&#xff0c;微软将以 687 亿美元的价格收购著名游戏制作和发行公…

java实现用户登录注册功能(用集合框架来实现)

需求&#xff1a;实现用户登录注册功能(用集合框架来实现) 分析&#xff1a; A:需求的类和接口 1.用户类 UserBean 2.用户操作方法接口和实现类 UserDao UserDaoImpl 3.测试类 UserTest B:各个类中的东西 1.用户类UserBean: …

第3次翻译了 Pandas 官方文档,叒写了这一份R万字肝货操作!

作者 | 黄伟呢来源 | 数据分析与统计学之美今天&#xff0c;我继续为大家讲述Pandas如何实现R语言的相关操作。由于 Pandas 旨在提供人们使用 R 进行的大量数据操作和分析功能&#xff0c;因此本页开始提供更详细的 R 语言及其与 Pandas 相关的许多第三方库的介绍。与 R 和 CRA…

PHP autoload机制详解

PHP autoload机制详解 转载自 jeakcccPHP autoload机制详解(1) autoload机制概述在使用PHP的OO模式开发系统时&#xff0c;通常大家习惯上将每个类的实现都存放在一个单独的文件里&#xff0c;这样会很容易实现对类进行复用&#xff0c;同时将来维护时也很便利。这 也是OO设计…

有关博客的一些断想

作者&#xff1a;朱金灿来源&#xff1a;http://blog.csdn.net/clever101随着微博、微信等短平快社交媒体的兴起&#xff0c;文字相对严肃的博客毫无疑问受到很大的冲击。我在想博客会不会因此而消亡呢。我相信不会&#xff0c;因为喜欢轻快的文字固然是人类的天性&#xff0c;…

pythonl学习笔记——爬虫的基本常识

1 robots协议 Robots协议&#xff08;也称为爬虫协议、机器人协议等&#xff09;的全称是“网络爬虫排除标准”&#xff08;Robots Exclusion Protocol&#xff09;&#xff0c;网站通过Robots协议告诉搜索引擎哪些页面可以抓取&#xff0c;哪些页面不能抓取。 如&#xff1a; …

hibernate相关收集

2019独角兽企业重金招聘Python工程师标准>>> 1、Hibernate SQL方言 如果出现如下错误&#xff0c;则可能是Hibernate SQL方言 (hibernate.dialect)设置不正确。 Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]last_ins…

盘一盘 2021 年程序员们喜欢的网站数据

作者 | 周萝卜来源 | 萝卜大杂烩世界上流量最大的网站有哪些&#xff0c;也许我们都能脱口而出&#xff0c;比如 Google&#xff0c;YouTube&#xff0c;Facebook 还有 PxxnHub 等等&#xff0c;今天我们就通过多个维度来看看&#xff0c;那些叱咤全球的流量网站&#xff01;数…

烽火18台系列之十一:刚需中的刚需——网站篡改监控

网站篡改事件近些年来越演越烈&#xff0c;其中包括政府、教育、金融、事业企业单位等。根据国家互联网应急响应中心发布的《2015年中国互联网网络安全报告》中指出&#xff0c;“2015年CNCERT/CC工检测到境内被篡改的网站数量为24550个&#xff0c;其中境内政府网站篡改数量为…

Http与RPC通信协议的比较

OSI网络结构的七层模型 各层的具体描述如下&#xff1a;第七层&#xff1a;应用层 定义了用于在网络中进行通信和数据传输的接口 - 用户程式&#xff1b;提供标准服务&#xff0c;比如虚拟终端、文件以及任务的传输 和处理&#xff1b; 第六层&#xff1a;表示层 掩…

基于 Python 和 OpenCV 构建智能停车系统

作者 | 努比来源 | 小白学视觉当今时代最令人头疼的事情就是找不到停车位&#xff0c;尤其是找20分钟还没有找到停车位。根据复杂性和效率的不同&#xff0c;任何问题都具有一个或多个解决方案。目前智能停车系统的解决方案&#xff0c;主要包括基于深度学习实现&#xff0c;以…

js获取鼠标位置

1.PageX/PageX:鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化2.clientX/clientY:鼠标在页面上可视区域的位置,从浏览器可视区域左上角开始,即是以浏览器滑动条此刻的滑动到的位置为参考点,随滑动条移动 而变化. 可是悲剧的是,PageX只有FF…

Lua保留指定小数位数

默认会四舍五入 比如&#xff1a;%0.2f 会四舍五入后&#xff0c;保留小数点后2位print(string.format("%.1f",0.26)) ---会输出0.3&#xff0c;而不是0.2 Lua保留一位小数 --- nNum 源数字 --- n 小数位数 function Tool. GetPreciseDecimal(nNum, n)if type(nNum)…

htaccess文件用法收集整理

1.时区设置有些时候&#xff0c;当你在PHP里使用date或mktime函数时&#xff0c;由于时区的不同&#xff0c;它会显示出一些很奇怪的信息。下面是解决这个问题的方法之一。就是设置你的服务器的时区。你可以在这里找到所有支持的时区的清单。 1.SetEnv TZ Australia/Melbourne …

手把手教你使用 YOLOV5 训练目标检测模型

作者 | 肆十二来源 | CSDN博客这次要使用YOLOV5来训练一个口罩检测模型&#xff0c;比较契合当下的疫情&#xff0c;并且目标检测涉及到的知识点也比较多。先来看看我们要实现的效果&#xff0c;我们将会通过数据来训练一个口罩检测的模型&#xff0c;并用pyqt5进行封装&#x…

数据仓库数据模型之:极限存储--历史拉链表

摘要: 在数据仓库的数据模型设计过程中&#xff0c;经常会遇到文内所提到的这样的需求。而历史拉链表&#xff0c;既能满足对历史数据的需求&#xff0c;又能很大程度的节省存储资源。在数据仓库的数据模型设计过程中&#xff0c;经常会遇到这样的需求&#xff1a;1. 数据量比较…

super的用法(带了解)

super的用法&#xff08;带了解&#xff09; super的用法&#xff08;带了解&#xff09;posted on 2018-05-11 21:31 leolaosao 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/leolaosao/p/9026686.html

Posted content type isn't multipart/form-data

版权声明&#xff1a;欢迎转载&#xff0c;请注明沉默王二原创。 https://blog.csdn.net/qing_gee/article/details/48712507 在有文件上传的表单提交过程中&#xff0c;搞不好就会报Posted content type isnt multipart/form-data的错误。 解决办法 <form class"form-…

CSDN 十大技术主题盘点-AI篇

关于2021&#xff0c;我们能看到的技术变化有很多。当云原生向下而生&#xff0c;当分布式数据库席卷而至&#xff0c;当低代码平台扩展了开发的边界&#xff0c;当万物互联蔚然成风……我们看到了太多在2021年形成的变化&#xff0c;但也能看到这些趋势非但没有结束&#xff0…

PHP编程问题集锦

1. Win32下apache2用get方法传递中文参数会出错 test.php?a你好&b你也好传递参数是会导致一个内部错误解决办法:"test.php?a".urlencode(你好)."&b".urlencode(你也好)2. win32下的session不能正常工作 php.ini默认的session.save_path /tmp 这…

jsonp详解

json相信大家都用的多&#xff0c;jsonp我就一直没有机会用到&#xff0c;但也经常看到&#xff0c;只知道是“用来跨域的”&#xff0c;一直不知道具体是个什么东西。今天总算搞明白了。下面一步步来搞清楚jsonp是个什么玩意。 同源策略 首先基于安全的原因&#xff0c;浏览器…