1

I have a template with predefined blocks. It looks like below:

<body>
  <div class="row">
    <div class="col-lg-6">
      <include name="position1" type="module">
    </div>
    <div class="col-lg-6">
      <include name="position2" type="module">
    </div>
  </div>
  <div class="row">
    <div class="col-lg-10">
      <include type="content">
    </div>
    <div class="col-lg-2">
      <include name="right" type="module">
    </div>
  </div> 
</body>

My predefined blocks:

-position1
-position2
-right
  1. I'm looking for the way to get the blocks above as array from my page;
  2. And small function to replace block with another content by block name;

For example:

function replace_block('position1', '<b>Replaced content</b>') {
   ... code

   And as output: 
   ...
   <div class="col-lg-6">
     <b>Replaced content</b>
   </div>
   ...
}

Thanks!

XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60
  • Use [a PHP DOM parser](http://simplehtmldom.sourceforge.net/). – Jay Blanchard Nov 20 '15 at 18:58
  • Have a look at this: http://stackoverflow.com/questions/4824503/php-domdocument-need-to-change-replace-an-existing-html-tag-w-a-new-one – Jonathan Kuhn Nov 20 '15 at 18:59
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – chris85 Nov 20 '15 at 19:05

1 Answers1

1

Try this:

    <?php

    echo replaceBlock('position1', 'this is a replacement string', $html);

    function replaceBlock($name,$replacement,$html) {
            $pattern = '/(<include name="' . $name . '".*?>)/';
            if (preg_match($pattern, $html, $matches)) {
                    $html = str_replace($matches[1],$replacement,$html);
            }
            return $html;
    }
Aric Watson
  • 126
  • 6