We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10313
    • 375 Posts
    Hello,

    I’m trying to code a small plugin that searches [*content*] OnWebPagePrerender and modifies it for my needs. It will be searching for <h1> and wrap a <div> around that <h1> and all subsequent content until end of content or until it finds the next <h1>. So I will be able to style blocks of content.

    My first approach is as follows:
    /**
     *  Finds blocks of content starting with <h1>
     *  Before each <h1> a <div class="contentblock"> will be inserted
     *  The closing </div> will be inserted before the next <h1> or at the end of content.  
     */
    
    $classname = "contentblock"; // name of the class to assign
    $e = &$modx->Event;
    $pattern = '/<h1>.*<\/h1>/';
    
    if($e->name == "OnWebPagePrerender"){
      $o = $modx->documentOutput; // get the output
      preg_match_all($pattern, $o, $out ); // get all images
      $replace = $out;
      
      $num = count($replace[0]);
      $o = "<p>$num gefunden.</p>$o";
      
      for($n=0;$n<count($replace[0]);$n++){
        $replace[0][$n] = "<div>" . $replace[0][$n] . "</div>";
        $o = str_replace($out[0][$n], $replace[0][$n], $o);
      }
    
      $modx->documentOutput = $o;
    }
    
    return;


    This is work in progress and actually only searches for <h1>..</h1>. But it finds each <h1> twice so that each <h1>..</h1> is surrounded TWICE by <div>..</div>. And this exactliy is my problem. Why is that so?

    Thanks
    Martin
      • 10313
      • 375 Posts
      Sorry, it was my fault, I was blind. My dummy page had doubled content. Both <h1> had the same content, so my plugin found two <h1> and replaced all <h1> that looked the same twice.

      Now I’m gonna refine my plugin.

      Thanks
      Martin