Skip to content
Advertisement

Add “first” and “last” classes to strings containing one or more tags in PHP

I have two strings I’m outputting to a page

JavaScript

What I’d like to do is turn them into this

JavaScript

I’m essentially trying to replicate the css equivalent of first-child and last-child, but I have to physically add them to the tags as I cannot use CSS. The strings are part of a MPDF document and nth-child is not supported on <p> tags.

I can iterate through the strings easy enough to split the <p> tags into an array

JavaScript

But once I have that array I’m struggling to find a nice clean way to append and prepend the first and last class to either end of the array. I end up with lots of ugly loops and array splicing that feels very messy.

I’m wondering if anyone has any slick ways to do this? Thank you 🙂

Edit Note: The two strings are outputting within a while(array) loop as they’re stored in a database.

Advertisement

Answer

You can index the node list with the item() method, so you can add the attribute to the first and last elements in the list.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement