Skip to content

Commit d9e304d

Browse files
committed
Refine the basic manipulation example
1 parent cc7dc2b commit d9e304d

File tree

1 file changed

+89
-14
lines changed
  • examples/basic-manipulation-filter-and-retrieval

1 file changed

+89
-14
lines changed

examples/basic-manipulation-filter-and-retrieval/index.php

+89-14
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,78 @@
5353
echo '<h2>Example 1</h2>';
5454
echo 'Add the attribute <code>class="cell"</code> to all <code>&lt;td&gt;</code> elements:';
5555

56+
echo '<pre><code>
57+
&lt;?php
58+
59+
echo html5qp($html, "td")
60+
-&gt;attr("class", "cell")
61+
-&gt;top("table")
62+
-&gt;html()
63+
</code></pre>';
64+
65+
echo 'This will output the following HTML:';
66+
67+
echo '<pre><code>';
68+
69+
echo htmlspecialchars(
70+
html5qp($html, 'td')
71+
->attr('class', 'cell')
72+
->top('table') // jump back up the DOM to the table
73+
->html() // get get HTML of the table
74+
);
75+
76+
echo '</code></pre>';
77+
78+
echo 'If you want to output a valid HTML document, use <code>top()</code> without an argument:';
79+
5680
echo '<pre><code>';
5781

5882
echo htmlspecialchars(
5983
html5qp($html, 'td')
6084
->attr('class', 'cell')
61-
->top() // return to <html> tag
62-
->innerHTML5() // get mark-up without <html>. Use ->html5() to return a valid HTML document (Doctype and all)
85+
->top()
86+
->html()
6387
);
6488

6589
echo '</code></pre>';
6690

6791
echo '<h2>Example 2</h2>';
68-
echo 'Use <code>html5qp($html)->find(\'#row2 > td:nth-child(2)\')->text();</code> to display the contents of the second <code>&lt;td&gt;</code> in the second <code>&lt;tr&gt;</code>: <br><strong>';
92+
echo 'Find and output the text of the second cell in the second row of the table:';
6993

70-
echo html5qp($html)
94+
$text = html5qp($html)
7195
->find('#row2 > td:nth-child(2)')
7296
->text();
7397

74-
echo '</strong>';
98+
echo '<pre><code>
99+
&lt;?php
100+
101+
echo html5qp($html)
102+
-&gt;find("#row2 > td:nth-child(2)")
103+
-&gt;text();
104+
105+
// Result: '. $text. '
106+
</code></pre>';
75107

76108
echo '<h2>Example 3</h2>';
77-
echo 'Append another row to the HTML and output the results:';
109+
echo 'Append an additional row at the end of the table:';
110+
echo '<pre><code>
111+
&lt;?php
112+
113+
echo html5qp($html, "td")
114+
-&gt;after("&lt;tr&gt;&lt;td&gt;seven&lt;/td&gt;&lt;td&gt;eight&lt;/td&gt;&lt;td&gt;nine&lt;/td&gt;&lt;/tr&gt;")
115+
-&gt;top("table")
116+
-&gt;html()
117+
</code></pre>';
118+
119+
echo 'This will output the following HTML:';
120+
78121
echo '<code><pre>';
79122

80123
echo htmlspecialchars(
81124
html5qp($html, 'tr:last')
82125
->after("\n\n\t<tr>\n\t\t<td>seven</td>\n\t\t<td>eight</td>\n\t\t<td>nine</td>\n\t</tr>")
83-
->top() // return to <html> tag
84-
->innerHTML5() // get mark-up without <html>. Use ->html5() to return a valid HTML document (Doctype and all)
126+
->top("table")
127+
->html()
85128
);
86129

87130
echo '</pre></code>';
@@ -93,33 +136,65 @@
93136
echo '<h2>Example 1</h2>';
94137
echo 'Add the attribute <code>class="item"</code> to all <code>&lt;desc&gt;</code> elements:';
95138

139+
echo '<pre><code>
140+
&lt;?php
141+
142+
echo qp($xml, "desc")
143+
-&gt;attr("class", "item)
144+
-&gt;top() // return to the &lt;categories&gt; tag
145+
-&gt;xml(); // output a valid XML document.
146+
</code></pre>';
147+
148+
echo 'This will output the following XML:';
149+
96150
echo '<pre><code>';
97151

98152
echo htmlspecialchars(
99153
qp($xml, 'desc')
100154
->attr('class', 'item')
101155
->top() // return to the <categories> tag
102-
->xml() // output a valid XML document. Use ->innerXML() to get the contents of <categories /> instead.
156+
->xml() // output a valid XML document
103157
);
104158

105159
echo '</code></pre>';
106160

161+
echo 'You can omit the XML declaration by setting the first argument to true: <code>-&gt;xml(true)</code>.';
162+
107163
echo '<h2>Example 2</h2>';
108-
echo 'Use <code>qp($xml)->find(\'categories > category:nth-child(3) desc\')->text();</code> to display the contents of the third <code>&lt;desc&gt;</code>: <br><strong>';
164+
echo 'Find and output the text of the third <code>&lt;desc&gt;</code> tag:';
109165

110-
echo qp($xml)
166+
$text = qp($xml)
111167
->find('categories > category:nth-child(3) desc')
112168
->text();
113169

114-
echo '</strong>';
170+
echo '<pre><code>
171+
&lt;?php
172+
173+
echo qp($xml)
174+
-&gt;find("categories > category:nth-child(3) desc")
175+
-&gt;text();
176+
177+
// Result: '.$text.'
178+
</code></pre>';
115179

116180
echo '<h2>Example 3</h2>';
117-
echo 'Append another category to the XML and output the results:';
181+
echo 'Append a category at the end of the group:';
182+
echo '<pre><code>
183+
&lt;?php
184+
185+
echo qp($xml, "category:last")
186+
-&gt;after("&lt;category name=\'Appended\'&gt;&lt;desc&gt;The appended node...&lt;/desc&gt;&lt;/category&gt;")
187+
-&gt;top()
188+
-&gt;xml()
189+
</code></pre>';
190+
191+
echo 'This will output the following HTML:';
192+
118193
echo '<code><pre>';
119194

120195
echo htmlspecialchars(
121196
qp($xml, 'category:last')
122-
->after("\n\n\t<category nam=\"Appended\">\n\t\t<desc>The appended node...</desc>\n\t</category>")
197+
->after("\n\n\t<category name=\"Appended\">\n\t\t<desc>The appended node...</desc>\n\t</category>")
123198
->top()
124199
->xml()
125200
);

0 commit comments

Comments
 (0)