We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14668
    • 11 Posts
    I’m a total and complete PHP newbie, and this is the first thing I’ve "written" on my own, but it’s proving useful so I thought I’d post it. The main problem I find with validation on a larger site is parsing all the errant ampersands somehow. Often that is the last wall between me and W3C validation. Frustrating!

    I just learned about input filters in MODx so I worked this up and it works great. I tried htmlspecialcharacters but it ended up being longer than needed because I had to replace back all the brackets and quotes.

    Create a snippet called "parseAmp" and paste this in.

    <?php
    
    // see if the input contains ampersands
    $amps = strpos($input,'&');
    
    if($amps === false) {
    	// amps not found, don't do anything
    }
    else {
    	// amps found, replace all instances of '&' with '&' 
    	// so if the two are mixed we will end up with all '&'
    	$input = str_replace("&",'&',$input);
    	// now replace all instances of '&' with '&' 
    	$input = str_replace("&",'&',$input);
    	echo $input;
    }


    Then I call it on the content, like [[*content:parseAmp]]

    Fwiw...

    Edit: I should probably specify that the main reason for this is because on the site, the editors have to paste a lot of content from other sites into tinyMCE, and so we end up with a lot of ampersands of both kinds, ’&’ and ’&amp;’

    Also mods, sorry if this post is in the wrong place... if so, please enlighten me. I read the guidelines and it seems it could go here?