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
source share
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
source

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

.*?, .* - .

@CanSpice:

, HTML. OP - <li>(?!.*<li>).*?</li> , . . OP

+1

Regexp

<li>(.*?)</li>
0

As you meet the HTML-text, I would suggest using sand iflags like this:

'~<li>(.*?)</li>~is'
  • s for DOTALL make dot .match all characters, including newline
  • I to match the case of ignoring
0
source

<?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
source
var a = '<ul>'+
'<li> some content</li>'+
'<li> some other content</li>'+
'<li> some other other content.</li>'+
'</ul>'

a.split("<li>") 
gives
["<ul>", " some content</li>", " some other content</li>", " some other other content.</li></ul>"]

From there we can choose whatever we want.

0
source

All Articles