Skip to content

Commit

Permalink
Refine the basic manipulation example
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejackson1 committed Apr 12, 2024
1 parent cc7dc2b commit d9e304d
Showing 1 changed file with 89 additions and 14 deletions.
103 changes: 89 additions & 14 deletions examples/basic-manipulation-filter-and-retrieval/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,78 @@
echo '<h2>Example 1</h2>';
echo 'Add the attribute <code>class="cell"</code> to all <code>&lt;td&gt;</code> elements:';

echo '<pre><code>
&lt;?php
echo html5qp($html, "td")
-&gt;attr("class", "cell")
-&gt;top("table")
-&gt;html()
</code></pre>';

echo 'This will output the following HTML:';

echo '<pre><code>';

echo htmlspecialchars(
html5qp($html, 'td')
->attr('class', 'cell')
->top('table') // jump back up the DOM to the table
->html() // get get HTML of the table
);

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

echo 'If you want to output a valid HTML document, use <code>top()</code> without an argument:';

echo '<pre><code>';

echo htmlspecialchars(
html5qp($html, 'td')
->attr('class', 'cell')
->top() // return to <html> tag
->innerHTML5() // get mark-up without <html>. Use ->html5() to return a valid HTML document (Doctype and all)
->top()
->html()
);

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

echo '<h2>Example 2</h2>';
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>';
echo 'Find and output the text of the second cell in the second row of the table:';

echo html5qp($html)
$text = html5qp($html)
->find('#row2 > td:nth-child(2)')
->text();

echo '</strong>';
echo '<pre><code>
&lt;?php
echo html5qp($html)
-&gt;find("#row2 > td:nth-child(2)")
-&gt;text();
// Result: '. $text. '
</code></pre>';

echo '<h2>Example 3</h2>';
echo 'Append another row to the HTML and output the results:';
echo 'Append an additional row at the end of the table:';
echo '<pre><code>
&lt;?php
echo html5qp($html, "td")
-&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;")
-&gt;top("table")
-&gt;html()
</code></pre>';

echo 'This will output the following HTML:';

echo '<code><pre>';

echo htmlspecialchars(
html5qp($html, 'tr:last')
->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>")
->top() // return to <html> tag
->innerHTML5() // get mark-up without <html>. Use ->html5() to return a valid HTML document (Doctype and all)
->top("table")
->html()
);

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

echo '<pre><code>
&lt;?php
echo qp($xml, "desc")
-&gt;attr("class", "item)
-&gt;top() // return to the &lt;categories&gt; tag
-&gt;xml(); // output a valid XML document.
</code></pre>';

echo 'This will output the following XML:';

echo '<pre><code>';

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

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

echo 'You can omit the XML declaration by setting the first argument to true: <code>-&gt;xml(true)</code>.';

echo '<h2>Example 2</h2>';
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>';
echo 'Find and output the text of the third <code>&lt;desc&gt;</code> tag:';

echo qp($xml)
$text = qp($xml)
->find('categories > category:nth-child(3) desc')
->text();

echo '</strong>';
echo '<pre><code>
&lt;?php
echo qp($xml)
-&gt;find("categories > category:nth-child(3) desc")
-&gt;text();
// Result: '.$text.'
</code></pre>';

echo '<h2>Example 3</h2>';
echo 'Append another category to the XML and output the results:';
echo 'Append a category at the end of the group:';
echo '<pre><code>
&lt;?php
echo qp($xml, "category:last")
-&gt;after("&lt;category name=\'Appended\'&gt;&lt;desc&gt;The appended node...&lt;/desc&gt;&lt;/category&gt;")
-&gt;top()
-&gt;xml()
</code></pre>';

echo 'This will output the following HTML:';

echo '<code><pre>';

echo htmlspecialchars(
qp($xml, 'category:last')
->after("\n\n\t<category nam=\"Appended\">\n\t\t<desc>The appended node...</desc>\n\t</category>")
->after("\n\n\t<category name=\"Appended\">\n\t\t<desc>The appended node...</desc>\n\t</category>")
->top()
->xml()
);
Expand Down

0 comments on commit d9e304d

Please sign in to comment.