We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23054
    • 62 Posts
    When using snippets with parameters - how do I mask special characters

    for instance the German "Ä" (Ä) does not lead to the desired result - how can it be masked ?

    [[snippet?&param=`Ä`]]
      • 10487 MODX Staff
      • 1,535 Posts
      HI persepolis,

      The best bet is to substitute the character in the snippet call (for example |umlaut| for Ä) and then do a string replace on that character inside the snippet. For example:
      $myVar = (isset($myVar)) ? str_replace('|umlaut|','Ä',$myVar) : false;

      Hope that helps,
      Garry
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 23054
        • 62 Posts
        Good idea - a more general solution is

        $myVar = (isset($myVar)) ? str_replace('###','&',$myVar) : false;
        
          • 26026
          • 2 Posts
          I wanted to allow special characters in snippets to make it easier to pass HTML. I modified a few lines inside modx/manager/includes/document.parser.class.inc.php:

          if (!empty ($currentSnippetParams)) {
          //$tempSnippetParams= str_replace("?", "", $currentSnippetParams); // This didn’t allow any params with ’?’. -Samer
          $tempSnippetParams = preg_replace(’/\?/’, ’’, $currentSnippetParams, 1); // Only replace the first ’?’. str_replace doesn’t have a limit in PHP for some reason. -Samer
          $splitter= "&";
          $tempSplitter = ’~!~MODX_SNIPPET_AMP~!~’; // Samer
          if (strpos($tempSnippetParams, "&") > 0)
          $tempSnippetParams= str_replace("&", "&", $tempSnippetParams);
          //$tempSnippetParams = html_entity_decode($tempSnippetParams, ENT_NOQUOTES, $this->config[’etomite_charset’]); //FS#334 and FS#456
          // example call: snippet? &param1=blah&param2= 
          $tempSnippetParams = preg_replace(’/’.$splitter.’(?!\w+=)/’, "$tempSplitter$1", $tempSnippetParams, -1); // Samer
          $tempSnippetParams= split($splitter, $tempSnippetParams);
          $snippetParamCount= count($tempSnippetParams);
          for ($x= 0; $x < $snippetParamCount; $x++) {
          if (strpos($tempSnippetParams[$x], ’=’, 0)) {
          if ($parameterTemp= explode("=", $tempSnippetParams[$x], 2)) { // limit added by Samer -- else very BAD if any = in snippet parameters!
          //$parameterTemp[1] = htmlentities($parameterTemp[1], ENT_NOQUOTES, $this->config[’etomite_charset’]);
          $parameterTemp[1] = str_replace($tempSplitter, $splitter, $parameterTemp[1]);
          $fp= strpos($parameterTemp[1], ’`’);
          $lp= strrpos($parameterTemp[1], ’`’);
          if (!($fp === false && $lp === false))
          $parameterTemp[1]= substr($parameterTemp[1], $fp +1, $lp -1);
          $parameter[$parameterTemp[0]]= $parameterTemp[1];
          }
          }
          }
          }

          All previously valid snippet calls still work as expected.