I have a str from witch I want to parse all <li></li>
tags, this is the string.
<li>Want this</li>DON'T WANT THIS<li>Want this</li>DON'T WANT THIS<li>Want this</li>...
This is the code that I'm using:
$my_text= array();
preg_match('/<li>(.*?)<\/li>/', $str, $my_text);
But it doesn't work. When I run it this is my_text array:
[0] => "<li>Want this</li>"
[1] => "Want this"
It only has 2 elements of 1000 of them.
All you need is using preg_match_all()
function, Something like this:
<?php
$str = "<li>Want this</li>DON'T WANT THIS<li>Want this</li>DON'T WANT THIS<li>Want this</li>";
preg_match_all('/<li>(.*?)<\/li>/', $str, $out);
echo '<pre>';
print_r($out);
Toto is correct, it is a really simple fix:
$str = "<li>Want this</li>DON'T WANT THIS<li>Want this</li>DON'T WANTTHIS<li>Want this</li>";
$my_text= array();
preg_match_all('/<li>(.*?)<\/li>/', $str, $my_text);
Use preg_match_all, as suggested above. It's really the best solution.
preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $input, $result, PREG_SET_ORDER);
The above example will remove any html tags from the input, not only li.
May I propose another solution, based on SimpleXML and xpath queries?
<?php
$string = "<html>
<li>Want this</li>DON'T WANT THIS<li>Want this</li>DON'T WANT THIS<li>Want this</li>
</html>";
$xml = simplexml_load_string($string);
# select only the li elements where the text is equal to...
$elements = $xml->xpath("//li[text() = 'Want this']");
print_r($elements);
// yields a list of your desired elements
?>
Hint: Your regex works as well, see a demo on regex101.com. Consider using other delimiters though:
$regex = '~<li>(.+?)</li>~';
preg_match_all($regex, $string, $matches);
print_r($matches);