regexp target last main li in list

I need to target the starting tag of the last top level LI in a list that may or may-not contain sublists in various positions - without using CSS or Javascript.

Is there a simple/elegant regexp that can help with this? I'm no guru w/ them, but it appears the need for greedy/non-greedy selectors when I'm selecting all the middle text (.*) / (.+) changes as nested lists are added and moved around in the list - and this is throwing me off.

$pattern = '/^(<ul>.*)<li>(.+<\/li><\/ul>)$/';
$replacement = '$1<li id="lastLi">$3';

Perhaps there is an easier approach?? converting to XML to target the LI and then convert back?

ie: Single Element

<ul>
    <li>TARGET</li>
</ul>

Multiple Elements

<ul>
    <li>foo</li>
    <li>TARGET</li>
</ul>

Nested Lists before end

<ul>
    <li>
        foo
        <ul>
            <li>bar</li>
        </ul>
    <li>
    <li>TARGET</li>
</ul>

Nested List at end

<ul>
    <li>foo</li>
    <li>
        TARGET
        <ul>
            <li>bar</li>
        </ul>
    </li>
</ul>

Answers


You should never use regex to parse HTML. Especially in this particular case (recursive tags).

Main reason overall is that HTML is not a regular language.

On top of the fact that HTML is not a regular language and can't be 100% correctly parsed with regex, the task to regex-parse HTML "well enough" is complicated enough that you're more likely than not going to have bugs in your code.

Instead, use a designated HTML parser.


Use an html parser not a regex.


XML conversion and DOM parsing is the easiest way if there is enough confidence about what kind of HTML data must be processed through.


Need Your Help

How can i display text after every 10 rows of an SQL query?

php sql mysqli

Say i have a query "select username from users". I want to output this query onto a PHP page but after every 10th row i want to display my own custom text. Any ideas? and it will continue from the ...

How to Set the Left and Top CSS Positioning of a DIV Element Dynamically Using JavaScript

javascript html css position

I want to create a div element manually and later add some CSS styling to it using JavaScript. I created a div element and changed it's style with JavaScript. The problem is, some CSS styles do not...