I have a string like below. I want to write a preg_match_all() function to get 'one' and 'two'. What change have to be done to the expression below to get the desired result?
$featureTab = "<li>one</li><li id='someId'>two</li>";
I have tried below code.
preg_match_all('/(?<=\<li\>)(.*?)(?=\<\/li\>)/', $featureTab ,$matches);
But it just returns 'one'. as the pregx only considers strings between the li tag not with id. Help me with a regular expression that will return both one and two.
You can simply use below regex
<li.*?>(.*?)<\/li>
Over here
`<li.*?>` here `(.*)` is to capture all attributes of `li` and `?` is to if no attributes is defined or not even space count also
As both has different li
structure
You can check it
Note: For
HTML/XML
parsing don't go for regex you can simply useDOMDocument
for same
You can use this regex:
<li[^>]*>(.*?)<\/li>
$re = '/<li[^>]*>(.*?)<\/li>/';
$str = '<li>one</li><li id=\'someId\'>two</li>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Check out the result here: https://3v4l.org/arFRq