to to to

How to update HTML code contained in variable with QueryPath

164 Views Asked by At

I am migrating some content into Drupal and want to use QueryPath to change image references from

<img src="example.jpg">

to

<img src="/sites/default/files/images/example.jpg">

I'm extracting the "main" div from the overall page HTML and putting it into a variable ($mainContent). Then I can get the image tags and update them. That's all working, but I can't figure out how to get the updated image tags back into my $mainContent variable.

This is my code:

$qp = qp($htmlCode); // full HTML page
$mainContent = $qp->top()->find('#main')->innerHTML(); // div containing main content

foreach (qp($mainContent, 'img') as $img) {
    $new_src = '/sites/default/files/images/' . $img->attr('src');
    $img->attr('src', $new_src);   // src attribute is updated

    $mainContent->XXXXX // ????? What to put here ????
}

After the second line in the foreach loop, the $img variable contains the updated src attribute for each image. However, I can't figure out what QueryPath syntax is necessary to fold the updated image tag back into the $mainContent variable. Should I just use the normal PHP "replace" functions to replace the old tag with the new? (I know there are lots of ways to do it but I want do it the "right" way!).

1

There are 1 best solutions below

0
Frank H. On

I figured it out for myself -- posting the answer in case it might be helpful for anyone else in future. The problem was, by getting the "innerHTML" value of $mainContent, I had set it up as a scalar variable rather than a QueryPath object.

The code below correctly applies the changes to the content of the variable.

$qp = qp($htmlCode); // full HTML page
$mainContent = $qp->top()->find('#main'); // div containing main content

foreach ($mainContent->top()->find('img') as $img) {
    $new_src = '/sites/default/files/images/' . $img->attr('src');
    $img->attr('src', $new_src);   // src attribute is updated
}
$output = $mainContent->top()->find('#main')->innerHTML();

Now, $output contains the HTML code of the "#main' div.