在Regex中搜索动态术语两次

I know I can refer in replacement to dynamic parts of the term in regex in PHP:

preg_replace('/(test1)(test2)(test3)/',"$3$2$1",$string);

(Somehow like this, I don't know if this is correct, but its not what I am looking for)

I want that in the regex, like:

preg_match_all("~<(.*)>.*</$1>~",$string,$matches);

The first part between the "<" and ">" is dynamic (so every tag existing in html and even own xml tags can be found) and i want to refer on that again in the same regex-term.

But it doesn't work for me. Is this even possible? I have a server with PHP 5.3

/edit:

my final goal is this:

if have a html-page with e. g. following source-code: HTML

<html>
  <head>
    <title>Titel</title>
  </head>
  <body>
    <div>
      <p>
        p-test<br />
        br-test
      </p>
      <div>
        <p>
          div-p-test
        </p>
      </div>
    </div>
  </body>
</html>

And after processing it should look like

$htmlArr = array(
    'html' => array(
            'head' => array('title' => 'Titel'),
            'body' => array(
                'div0' => array(
                    'p0' => 'p-test<br />br-test',
                    'div1' => array(
                        'p1' => 'div-p-test'
                    )
                )
            )
    ));

Placeholders in the replacement string use the $1 syntax. In the regex itself they are called backreferences and follow the syntax \1 backslash and number.
http://www.regular-expressions.info/brackets.html

So in your case:

preg_match_all("~<(.*?)>.*?</\\1>~",$string,$matches);

The backslash is doubled here, because in PHP strings the backslash escapes itself. (In particular for double quoted strings, else it would become an ASCII symbol.)