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

    Here’s a snippet I made based on a bit of research on how to clean out the content variable of unwanted elements. Useful if you’re retrieving partial information from your resource’s content section for use in summary sections. Especially useful with the :limit or :ellipse output filter and if you don’t want to use the introtext portion when you create new resources (for lazy bums who like magic summaries straight from their content, grin).

    The reason why I made this is because the strip_tags output filter still leave some junk in the output (like content left from stripped tags - h1, h2, h3).

    Instructions:

    Make a new snippet called stripMediaTags. Enter the code below.

    if(!function_exists('tagContentCleaner'))
    {
    	function tagContentCleaner($tag_container, $result) {
    		$tags = explode(',', $tag_container);
    
    		foreach ($tags as $tag) {
    			$preg_code = '|\<'. $tag .'.*\>(.*\n*)\</'. $tag .'\>|isU';
    			$result = preg_replace($preg_code, '', $result);
    		}
    		return $result;
    
    	}
    }
    
    $result = $scriptProperties['input'];
    
    /*
    Define the tags with content you want to destroy.
    Make sure the tags here do not contradict with the ones 
    you've got in the allowed tags that's used by strip_tags.
    */
    
    $taglist = "h1,h2,h3,h4,h5,h6,span,div";
    $allowedtags = "<p><a>";
    
    /*
    Commence cleaning operation.
    */
    
    
    $result = tagContentCleaner($taglist, $result);
    $result = strip_tags($result, $allowedtags);
    
    /*
    Clean out remaining empty tags.
    */
    
    $result = preg_replace('#<(\w+)[^>]*></\1>#i','', $result);
    
    return $result;
    


    Use with placeholders that can use IO filters:

    [[*content:stripMediaTags]]


    Edit the $taglist for HTML tags you want to get rid of. Make sure to keep a comma in between the tag names. This will first get rid of any element within the taglist and whatever content’s in between. Also make sure this doesn’t contradict with the succeeding strip_tags function’s allowed tags. The last code gets rid of any remaining tags that are empty. Forgot where I got the regex’s from, but kudos to them.

    If anyone wants to improve / expand on this, feel free to do so.
      • 3749
      • 24,544 Posts
      Nice.

      I think this might do the same thing, but I’m not sure.

      :strip=`<p><a>`
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting