如何将数据从DOMDocument传递到regexp?

Using the following code I get "img" tags from some html and check them if they are covered with "a" tags. Later if current "img" tag is not part of the "a" ( hyperlink ) I want to do cover this img tag into "a" tag adding hyperlinks start ending tag plus setting to target. For this I want the whole "img" tags html to work with.

Question is how can I transfer "img" tags html into regexp. I need some php variable in regexp to work with the place is marked with ??? signs.

$doc = new DOMDocument();
$doc->loadHTML($article_header);

$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {

if ($img->parentNode->tagName != "a") {

preg_match_all("|<img(.*)\/>|U", ??? , $matches, PREG_PATTERN_ORDER);

                                      }
                        }

You do not want to use regex for this. You already have a DOM, so use it:

foreach ($imgs as $img) {
  $container = $img->parentNode;

  if ($container->tagName != "a") { 
    $a = $doc->createElement("a");
    $a->appendChild( $img->cloneNode(true) );
    $container->replaceChild($a, $img);
  }
}

see documentation on