We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    I am rewriting the Archives parser for Ditto to allow more intricate templating but have run into a regex issue. Could someone who actually knows regex help rewrite the following line to parse this:

    [++tpl++]
    asfadsasdasdasdsdsad
    [++/tpl++]


    I am currently using this code which pulls everything between [++ and ++] but it needs to get both that and everything between the opening and closing tag.

    preg_match_all('~\[\+\+(.*?)\+\+\]~', $templates['tplArch'], $matches);




      • 15987
      • 786 Posts
      this should work:

      preg_match_all('/\[\+\+(.*?)\+\+\](.*?)\[\+\+(.*?)\+\+\]/is', $templates['tplArch'], $matches);
        • 18397
        • 3,250 Posts
        $templates['tplArch'] ="
        <h3>Archives</h3>
        <div id=\"ditto_archivelist\">
        	<ul id=\"ditto_archive_ul\">
        		[++year++]
        		<li><span class=\"ditto_year\">[+year+]
        			[++month++]
        			<li><span class=\"ditto_month\">[+month+]
        				<ul>
        					[++item++]<li><a href=\"[~[+id+]~]\">[+title+]</a> (<span class=\"ditto_date\">[+date+]</span>)</li>[++/item++]
        				</ul>
        			</li>
        			[++/month++]
        		</li>
        		[++/year++]
        	</ul>
        	[++/year++]
        </div>";
        
        function parseArchiveTPL($templates){
        	preg_match_all('/\[\+\+(.*?)\+\+\](.*?)\[\+\+(.*?)\+\+\]/i', $templates['tplArch'], $matches);
        	return $matches;
        }
        
        $ktext = print_r(parseArchiveTPL($templates),1);
        return "<pre>$ktext</pre>";
        
        


        This test does not return each individual template by [++value++], only the item template. Also, note that the closing tag has a / in it.
          • 15987
          • 786 Posts
          I didn’t realize you were going to have nested items, I will give it another try smiley

          you also left off the ’s’ on the end of the regex. without it you get no results.
            • 18397
            • 3,250 Posts
            I get the following results with the s:

            Array
            (
                [0] => Array
                    (
                        [0] => 
            		<li><span class="ditto_year">
            			
                        [1] => <li><a href="November-2005.html">November 2005</a> (<span class="ditto_date">23-Nov-05 12:29</span>)</li>
                        [2] => 
            		</li>
            		
                    )
            
                [1] => Array
                    (
                        [0] => year
                        [1] => item
                        [2] => /month
                    )
            
                [2] => Array
                    (
                        [0] => 
            		<li><span class="ditto_year">
            			
                        [1] => <li><a href="November-2005.html">November 2005</a> (<span class="ditto_date">23-Nov-05 12:29</span>)</li>
                        [2] => 
            		</li>
            		
                    )
            
                [3] => Array
                    (
                        [0] => month
                        [1] => /item
                        [2] => /year
                    )
            
            )
            
            
              • 15987
              • 786 Posts
              Mark,
              does this do the trick for you? I’m not sure if there is a better way to do this but I split it out into two seperate regex calls.

              $templates['tplArch'] ="
              <h3>Archives</h3>
              <div id=\"ditto_archivelist\">
              	<ul id=\"ditto_archive_ul\">
              		[++year++]
              		<li><span class=\"ditto_year\">[+year+]
              			[++month++]
              			<li><span class=\"ditto_month\">[+month+]
              				<ul>
              					[++item++]<li><a href=\"[~[+id+]~]\">[+title+]</a> (<span class=\"ditto_date\">[+date+]</span>)</li>[++/item++]
              				</ul>
              			</li>
              			[++/month++]
              		</li>
              		[++/year++]
              	</ul>
              </div>";
              
              function parseArchiveTPL($templates){
              	preg_match_all('/\[\+\+(.*?)\+\+\]/is', $templates['tplArch'], $matches);
              	return $matches;
              }
              
              $placeHolders = parseArchiveTPL($templates);
              
              $numPlaceHolders = count($placeHolders[1]) / 2 - 1;
              
              $extractedTemplates = array();
              
              for ($i = $numPlaceHolders; $i >= 0; $i--) {
                  $regExp = '/\[\+\+' . $placeHolders[1][$i] . '\+\+\](.*?)\[\+\+\/' . $placeHolders[1][$i] . '\+\+\]/is';
                  preg_match_all($regExp, $templates['tplArch'], $matches);
                  $extractedTempaltes[$placeHolders[1][$i]] = $matches[1];
              }
              
              $ktext .= print_r($extractedTempaltes,1);
              
              return "<pre>$ktext</pre>";
              
                • 18397
                • 3,250 Posts
                Thanks! That works great.
                  • 15987
                  • 786 Posts
                  No problem, glad to help smiley