Need help parsing a nested unordered list

Hello,
I have the following string:


$text = '
<ul>
    <li>Content Content Content
        <ul>
            <li>Nested Content Content</li>
            <li>Nested Content Conten</li>
            <li>Nested Content Conten</li>
        </ul>
    </li>
    <li>Content Content Content </li>
    <li>Content Content Content</li>
</ul>';

I need help building a function that will convert the li tags to p tags, but at the same time indent the nested content with an inline styles so the output would look like the following:

Output:


<p>Content Content Content</p> 
<p style="margin-left:10px">Nested Content Content</p>
<p style="margin-left:10px">Nested Content Content</p>
<p style="margin-left:10px">Nested Content Content</p>
<p>Content Content Content</p>
<p>Content Content Content</p>

Any help is greatly appreciated.

Using array and algorithm like FIFO to convert open/close tag to p tag. Is that ok?

Thanks prajapati!

Use DOM to parse HTML and then work with it. Use functions like children() to identify <li> inside <li>.

See some examples of html parsing in php.

Refer this to know the syntax etc.

Anyone?

Actually, I need this for a dynamically generated Word document. In the Word document the unordered lists are p tags with office XML styles attached to them.

There must be some reason why you wouldn’t simply use CSS to style your unordered list and achieve the same result…