Search code examples
phpdomdrupalsimple-html-dom

Get all img tags and add <a href=""/> between


I want to get all img tags from drupal site and then add a tag between.

I try the below:

 $html_img = new simple_html_dom();
  // Load HTML from a string.
 $html_img->load($node->body[LANGUAGE_NONE][0]['value']);
  // Remove all plain text fragments.
  foreach ($html_img->find('img') as $e ) {
    $e = "<a href='$node_url'>$e</a>";
  }

With above code I take all img tags from drupal but the $e = "<a href='$node_url'>$e</a>"; doesn't put a link to img tags.


Solution

  • $dom = new DOMDocument;
    $dom->loadXML($xml);
    $images = $dom->getElementsByTagName('img');
    foreach ($images as $img) {
        echo "<a href='#'>$img</a>";
    }
    

    DomDocument.

    Was the first result in Google.