PHP function to edit HTML using php DOM Parser [closed]

Want to improve this question? Update the question so it’s on-topic for WordPress Development Stack Exchange.

Closed 5 days ago.

In a page I need to find a text or a HTML element by class. If I find this text (or element), in the page I show or hide specific div.classes.

HTML:

I would use a php function in functions.php to do this, because I already use js but it works badly.

You can see a method to filter data like this (ref. here):

Can I edit the above function for my needs? Thank you.