How can I match content between specific <li> tags?
How can I match all the parameters of the <li> tags in the bottom HTML:
<ul>
<li> some content</li>
<li> some other content</li>
<li> some other other content.</li>
</ul>
This expression does not work:
<Li> </ l (*.); / & Li GT;
Because it returns:
some content</li>
<li> some other content</li>
<li> some other other content.
What is the content between the first <li> and the last </li>
+3
7 answers
Regular expressions are greedy in nature. Make it not greedy by adding ?.
<li>(.*?)</li>
Note. I would recommend DOM Parser for such a thing. Check out the PHP DOMDocument .
+6
Someone please associate a Parser question with a regex expression ...
, HTML, HTML.
, , ..:
<?php
function innerHTML($node) {
$doc = new DOMDocument();
foreach ($node->childNodes as $child) {
$doc->appendChild($doc->importNode($child, true));
}
return $doc->saveHTML();
}
$string = "<ul>
<li> some content</li>
<li> some other content</li>
<li> some other other content.</li>
</ul>";
$document = new DOMDocument();
$document->loadHTML($string);
$ul = $document->getElementsByTagName("ul");
foreach ($ul as $element) {
print innerHTML($element);
}
?>
, . :
<?php
$string = "<ul>
<li> some content</li>
<li> some other content</li>
<li> some other other content.</li>
</ul>";
$document = new DOMDocument();
$document->loadHTML($string);
$ul = $document->getElementsByTagName("li");
foreach ($ul as $element) {
print $element->nodeValue;
}
?>
+2
<?php
$str = '<ul>
<li> some content</li>
<li> some other content</li>
<li> some other other content.</li>
</ul>';preg_match_all ('/ <li> ([^ <] +) </li> / i', $ str, $ r); print_r ($ r [1]); ? >
Conclusion:
`Array
(
[0] => some content
[1] => some other content
[2] => some other other content.
)
`0