mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 08:58:56 +00:00
Added <screen> output for examples.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@301048 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
54028b9d81
commit
ea2023e453
9 changed files with 273 additions and 28 deletions
|
@ -56,16 +56,27 @@ XML;
|
|||
a string or number from a basic XML document.
|
||||
<example>
|
||||
<title>Getting <literal><plot></literal></title>
|
||||
<programlisting role="php"><![CDATA[
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
include 'example.php';
|
||||
|
||||
$xml = new SimpleXMLElement($xmlstr);
|
||||
|
||||
echo $xml->movie[0]->plot; // "So this language. It's like..."
|
||||
echo $xml->movie[0]->plot;
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
|
||||
So, this language. It's like, a programming language. Or is it a
|
||||
scripting language? All is revealed in this thrilling horror spoof
|
||||
of a documentary.
|
||||
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -81,10 +92,16 @@ include 'example.php';
|
|||
|
||||
$xml = new SimpleXMLElement($xmlstr);
|
||||
|
||||
echo $xml->movie->{'great-lines'}->line; // "PHP solves all my web problems"
|
||||
echo $xml->movie->{'great-lines'}->line;
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
PHP solves all my web problems
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -101,14 +118,21 @@ include 'example.php';
|
|||
|
||||
$xml = new SimpleXMLElement($xmlstr);
|
||||
|
||||
/* For each <movie> node, we echo a separate <plot>. */
|
||||
foreach ($xml->movie as $movie) {
|
||||
echo $movie->plot, '<br />';
|
||||
/* For each <character> node, we echo a separate <name>. */
|
||||
foreach ($xml->movie->characters->character as $character) {
|
||||
echo $character->name, ' played by ', $character->actor, PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Ms. Coder played by Onlivia Actora
|
||||
Mr. Coder played by El ActÓr
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<note>
|
||||
|
@ -149,6 +173,12 @@ foreach ($xml->movie[0]->rating as $rating) {
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
7 thumbs up5 stars
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -159,8 +189,9 @@ foreach ($xml->movie[0]->rating as $rating) {
|
|||
function that requires a string, you must cast it to a string using
|
||||
<literal>(string)</literal>. Otherwise, PHP treats the element as an object.
|
||||
</simpara>
|
||||
<programlisting role="php"><![CDATA[
|
||||
<?php
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
include 'example.php';
|
||||
|
||||
$xml = new SimpleXMLElement($xmlstr);
|
||||
|
@ -169,10 +200,16 @@ if ((string) $xml->movie->title == 'PHP: Behind the Parser') {
|
|||
print 'My favorite movie.';
|
||||
}
|
||||
|
||||
htmlentities((string) $xml->movie->title);
|
||||
echo htmlentities((string) $xml->movie->title);
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
My favorite movie.PHP: Behind the Parser
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -182,14 +219,23 @@ htmlentities((string) $xml->movie->title);
|
|||
Two SimpleXMLElements are considered different even if they point to the
|
||||
same element since PHP 5.2.0.
|
||||
</simpara>
|
||||
<programlisting role="php"><![CDATA[
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
include 'example.php';
|
||||
|
||||
$el1 = new SimpleXMLElement($xmlstr);
|
||||
$el2 = new SimpleXMLElement($xmlstr);
|
||||
var_dump($el1 == $el2); // false since PHP 5.2.0
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
bool(false)
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -203,10 +249,11 @@ var_dump($el1 == $el2); // false since PHP 5.2.0
|
|||
<![CDATA[
|
||||
<?php
|
||||
include 'example.php';
|
||||
|
||||
$xml = new SimpleXMLElement($xmlstr);
|
||||
|
||||
foreach ($xml->xpath('//character') as $character) {
|
||||
echo $character->name, 'played by ', $character->actor, '<br />';
|
||||
echo $character->name, 'played by ', $character->actor, PHP_EOL;
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
|
@ -215,6 +262,13 @@ foreach ($xml->xpath('//character') as $character) {
|
|||
'<literal>//</literal>' serves as a wildcard. To specify absolute
|
||||
paths, omit one of the slashes.
|
||||
</simpara>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Ms. Coder played by Onlivia Actora
|
||||
Mr. Coder played by El ActÓr
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -236,10 +290,37 @@ echo $xml->asXML();
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
<simpara>
|
||||
The above code will output a new XML document, just like the original,
|
||||
except that the new XML will change Ms. Coder to Miss Coder.
|
||||
</simpara>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
<?xml version="1.0" standalone="yes"?>
|
||||
<movies>
|
||||
<movie>
|
||||
<title>PHP: Behind the Parser</title>
|
||||
<characters>
|
||||
<character>
|
||||
<name>Miss Coder</name>
|
||||
<actor>Onlivia Actora</actor>
|
||||
</character>
|
||||
<character>
|
||||
<name>Mr. Coder</name>
|
||||
<actor>El ActÓr</actor>
|
||||
</character>
|
||||
</characters>
|
||||
<plot>
|
||||
So, this language. It's like, a programming language. Or is it a
|
||||
scripting language? All is revealed in this thrilling horror spoof
|
||||
of a documentary.
|
||||
</plot>
|
||||
<great-lines>
|
||||
<line>PHP solves all my web problems</line>
|
||||
</great-lines>
|
||||
<rating type="thumbs">7</rating>
|
||||
<rating type="stars">5</rating>
|
||||
</movie>
|
||||
</movies>
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -266,10 +347,37 @@ echo $xml->asXML();
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
<simpara>
|
||||
The above code will output an XML document based on the original but
|
||||
having a new character and rating.
|
||||
</simpara>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
<?xml version="1.0" standalone="yes"?>
|
||||
<movies>
|
||||
<movie>
|
||||
<title>PHP: Behind the Parser</title>
|
||||
<characters>
|
||||
<character>
|
||||
<name>Ms. Coder</name>
|
||||
<actor>Onlivia Actora</actor>
|
||||
</character>
|
||||
<character>
|
||||
<name>Mr. Coder</name>
|
||||
<actor>El ActÓr</actor>
|
||||
</character>
|
||||
<character><name>Mr. Parser</name><actor>John Doe</actor></character></characters>
|
||||
<plot>
|
||||
So, this language. It's like, a programming language. Or is it a
|
||||
scripting language? All is revealed in this thrilling horror spoof
|
||||
of a documentary.
|
||||
</plot>
|
||||
<great-lines>
|
||||
<line>PHP solves all my web problems</line>
|
||||
</great-lines>
|
||||
<rating type="thumbs">7</rating>
|
||||
<rating type="stars">5</rating>
|
||||
<rating type="mpaa">PG</rating></movie>
|
||||
</movies>
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -283,11 +391,11 @@ echo $xml->asXML();
|
|||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
$dom = new domDocument;
|
||||
$dom = new DOMDocument;
|
||||
$dom->loadXML('<books><book><title>blah</title></book></books>');
|
||||
if (!$dom) {
|
||||
echo 'Error while parsing the document';
|
||||
exit;
|
||||
echo 'Error while parsing the document';
|
||||
exit;
|
||||
}
|
||||
|
||||
$s = simplexml_import_dom($dom);
|
||||
|
@ -296,6 +404,12 @@ echo $s->book[0]->title;
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
blah
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</section>
|
||||
|
|
|
@ -70,10 +70,16 @@ if (!$dom) {
|
|||
|
||||
$s = simplexml_import_dom($dom);
|
||||
|
||||
echo $s->book[0]->title; // blah
|
||||
echo $s->book[0]->title;
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
blah
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</refsect1>
|
||||
|
|
|
@ -119,7 +119,7 @@ XML;
|
|||
|
||||
$xml = simplexml_load_string($string);
|
||||
|
||||
var_dump($xml);
|
||||
print_r($xml);
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
|
|
|
@ -93,6 +93,48 @@ echo $sxe->asXML();
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs.similar;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
<?xml version="1.0" standalone="yes"?>
|
||||
<movies type="documentary">
|
||||
<movie>
|
||||
<title>PHP: Behind the Parser</title>
|
||||
<characters>
|
||||
<character>
|
||||
<name>Ms. Coder</name>
|
||||
<actor>Onlivia Actora</actor>
|
||||
</character>
|
||||
<character>
|
||||
<name>Mr. Coder</name>
|
||||
<actor>El ActÓr</actor>
|
||||
</character>
|
||||
</characters>
|
||||
<plot>
|
||||
So, this language. It's like, a programming language. Or is it a
|
||||
scripting language? All is revealed in this thrilling horror spoof
|
||||
of a documentary.
|
||||
</plot>
|
||||
<great-lines>
|
||||
<line>PHP solves all my web problems</line>
|
||||
</great-lines>
|
||||
<rating type="thumbs">7</rating>
|
||||
<rating type="stars">5</rating>
|
||||
</movie>
|
||||
<movie>
|
||||
<title>PHP2: More Parser Stories</title>
|
||||
<plot>This is all about the people who make it work.</plot>
|
||||
<characters>
|
||||
<character>
|
||||
<name>Mr. Parser</name>
|
||||
<actor>John Doe</actor>
|
||||
</character>
|
||||
</characters>
|
||||
<rating type="stars">5</rating>
|
||||
</movie>
|
||||
</movies>
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</refsect1>
|
||||
|
|
|
@ -73,7 +73,7 @@
|
|||
<?php
|
||||
|
||||
include 'example.php';
|
||||
|
||||
|
||||
$sxe = new SimpleXMLElement($xmlstr);
|
||||
$sxe->addAttribute('type', 'documentary');
|
||||
|
||||
|
@ -94,6 +94,48 @@ echo $sxe->asXML();
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs.similar;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
<?xml version="1.0" standalone="yes"?>
|
||||
<movies type="documentary">
|
||||
<movie>
|
||||
<title>PHP: Behind the Parser</title>
|
||||
<characters>
|
||||
<character>
|
||||
<name>Ms. Coder</name>
|
||||
<actor>Onlivia Actora</actor>
|
||||
</character>
|
||||
<character>
|
||||
<name>Mr. Coder</name>
|
||||
<actor>El ActÓr</actor>
|
||||
</character>
|
||||
</characters>
|
||||
<plot>
|
||||
So, this language. It's like, a programming language. Or is it a
|
||||
scripting language? All is revealed in this thrilling horror spoof
|
||||
of a documentary.
|
||||
</plot>
|
||||
<great-lines>
|
||||
<line>PHP solves all my web problems</line>
|
||||
</great-lines>
|
||||
<rating type="thumbs">7</rating>
|
||||
<rating type="stars">5</rating>
|
||||
</movie>
|
||||
<movie>
|
||||
<title>PHP2: More Parser Stories</title>
|
||||
<plot>This is all about the people who make it work.</plot>
|
||||
<characters>
|
||||
<character>
|
||||
<name>Mr. Parser</name>
|
||||
<actor>John Doe</actor>
|
||||
</character>
|
||||
</characters>
|
||||
<rating type="stars">5</rating>
|
||||
</movie>
|
||||
</movies>
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</refsect1>
|
||||
|
|
|
@ -71,11 +71,26 @@ XML;
|
|||
|
||||
$xml = new SimpleXMLElement($string);
|
||||
|
||||
echo $xml->asXML(); // <?xml ... <a><b><c>text</c><c>stuff</c> ...
|
||||
echo $xml->asXML();
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
<?xml version="1.0"?>
|
||||
<a>
|
||||
<b>
|
||||
<c>text</c>
|
||||
<c>stuff</c>
|
||||
</b>
|
||||
<d>
|
||||
<c>code</c>
|
||||
</d>
|
||||
</a>
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
@ -91,11 +106,17 @@ echo $xml->asXML(); // <?xml ... <a><b><c>text</c><c>stuff</c> ...
|
|||
$result = $xml->xpath('/a/b/c');
|
||||
|
||||
while(list( , $node) = each($result)) {
|
||||
echo $node->asXML(); // <c>text</c> and <c>stuff</c>
|
||||
echo $node->asXML();
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
<c>text</c><c>stuff</c>
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</refsect1>
|
||||
|
|
|
@ -115,6 +115,12 @@ echo $sxe->movie[0]->title;
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
PHP: Behind the Parser
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
include 'example.php';
|
||||
$sxe = new SimpleXMLElement($xmlstr);
|
||||
|
||||
echo $sxe->getName() . "\n";
|
||||
|
@ -48,6 +48,13 @@ foreach ($sxe->children() as $child)
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
movies
|
||||
movie
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</refsect1>
|
||||
|
|
|
@ -102,6 +102,13 @@ foreach ($result as $title) {
|
|||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Chapter 1
|
||||
Chapter 2
|
||||
]]>
|
||||
</screen>
|
||||
<para>
|
||||
Notice how the XML document shown in the example sets a namespace with a
|
||||
prefix of <literal>chap</literal>. Imagine that this document (or another
|
||||
|
|
Loading…
Reference in a new issue