Find and Replace with QueryPath

728 Views Asked by At

I have a string assigned to a variable of $output. I want to use QueryPath to iterate through the string and add a class to each matched element.

Currently I have:

$output = qp($output)->find('table')->addclass('table')->html();

The only issue with this is it removes the content of $output before the first match. Is there a way of using QueryPath to find the match and then add the class while keeping the contents of $output intact?

EDIT:

Pseudo code:

$output = '<table class="temp"><blah></blah></table>';

Magic QueryPath

$output = '<table class="temp blah"><blah></blah></table>';
1

There are 1 best solutions below

1
On

try removing html()

$output = qp($output)->find('table')->addclass('table');

For displaying:

$output->writeHTML();

html():: Replaces the child content of the current element or elements in an HTML document