We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33982 ☆ A M B ☆
    • 476 Posts
    So I’m sure this is probably an easy Javascript code (unfortunately I really don’t know Javascript nor PHP), but I’m trying to remove special characters from a template variable’s output. In this case, I want to remove any special characters the end user may input. When someone enters information into "Title" field in the document, I want the output (in this case, the [*pagetitle*] T.V. as seen below) to remove the special characters (i.e. ’, #, *, ", etc.). Here’s my code below:

            <form action="https://cherubscottage.foxycart.com/cart" class="foxycart" method="post">
    	<input type="hidden" name="name" value="[*pagetitle*]" />
    	<input type="hidden" name="price" value="[*price*]" />
    	<input type="hidden" name="storeid" value="[*storeid*]" />
            <input type="hidden" name="weight" value="[*weight*]" />
    	<input id="shoppingcartlogo" type="image" src="assets/templates/cherubscottage/layout/add-to-cart.gif" alt="Add to Basket" />
    	</form>
            


    I was playing with some Javascript, trying to modify it (but again, I know really nothing of Javascript), but here’s the basic idea:
         function filterNum(str) {
              re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
              // remove special characters like "$" and "," etc...
              return str.replace(re, "");
         }
        

    Is there a way in MODx to remove special characters from T.V.’s output? Or do I just need Javascript (and if so, any recommendations for that coding?)?

    Thanks!
      Precision Web Development ... SmashStack.com
      • 33982 ☆ A M B ☆
      • 476 Posts

      You could e.g. write a little snippet that takes the TV output and strips the unwanted characters, and only returns the "clean" string (preg_replace, str_replace...)

      Hey ganeshXL,

      Thank you for your reply. I guess the other thing I forgot to mention in my posting is that I don’t know any php...so when you say write a little snippet, that would be beyond me at this time. :’(

      Is there a snippet already that you know for this? I just came across this online...think it would work in this case?

      http://www.emanueleferonato.com/2006/07/27/strip-non-alphanumeric-characters-from-a-string-with-php/

      Thanks!
        Precision Web Development ... SmashStack.com
        • 33982 ☆ A M B ☆
        • 476 Posts
        Thanks again ganeshXL for your "leading" me through this process. I guess here’s where I’m stumped. So this was your advice...create a snippet and call it like so:


        <input type="hidden" name="name" value="[!cleanTV? &myTV=`[*pagetitle*]`!]" />

        I pointed to the example of the link above. I tried it out and here’s where I’m stuck (again, b/c I know no PHP). I came up with this and it’s not working. I created a snippet and put this in the snippet:


        <?php
        $string = "myTV";
        $new_string = preg_replace("/[^a-zA-Z0-9s]/", "", $string);
        echo $new_string
        ?>

        Where am I wrong...or rather I should be asking, how do I do it right?

        Thanks again for your insight and help.
          Precision Web Development ... SmashStack.com
          • 17883
          • 1,039 Posts
          Try this:

          <?php
          $string = isset ($myTV) ? $tmyTV : "";
          $new_string = preg_replace("/[^a-zA-Z0-9s]/", "", $string);
          return $new_string
          ?>
          
            • 33982 ☆ A M B ☆
            • 476 Posts
            Thanks for everyone’s input, but still I’m not getting any results. embarrassed

            So I guess I’ll put all the information possible to make sure I’m not excluding something.

            So I have a template with this T.V.

            <div id="buynowbutton">
            [*modifierslayout*]
            </div><!--Buy Now Button-->
            


            The T.V. is controlled by the admin and the T.V. (modifierslayout) controls what layout the admin wants to appear on the page. The choices are seen here:

            Standard (no modifiers)=={{standardnomod}}||Standard (w/external link)=={{standardexternal}}||Modifiers for size (drop down menu)=={{modifiers2}}
            


            So once the admin selects "Modifiers for size (drop down menu)" in the T.V. area and they then put in their information. That information is given to this chunk (the "Modifiers2" chunk):

            <input type="hidden" name="name" value="[!cleanTV? &myTV=`[*pagetitle*]`!]" />
            


            with cleanTV as my new snippet (thank you MadeMyDay for your input):

            <?php
            $string = isset ($myTV) ? $tmyTV : "";
            $new_string = preg_replace("/[^a-zA-Z0-9s]/", "", $string);
            return $new_string
            ?>
            


            But still no love...it’s still not working...not sure why the snippet is not working in this scenario.

            Any insight would be greatly appreciated!

            And by the way, here’s where the site is live with an example. If you look on the product on the right, when you click on the "Buy Now" button, the shopping cart is stopped b/c the product name can not be passed to it. If you look at the source code, you’ll see there is no value for "product" b/c nothing is coming out. It’s not that the snippet is not removing the non alphanumeric values, it’s just nothing is coming out whatsoever. Here’s an example product page:

            http://66.240.226.230/womens-jacket-navy
              Precision Web Development ... SmashStack.com
              • 29635
              • 361 Posts
              Haha, you have a little typo wink Bonus points if you can find it.
                Need MODx Ecommerce? Try FoxyCart!
                • 33982 ☆ A M B ☆
                • 476 Posts
                Ok, finally got it to work...thanks for the help everyone!

                <?php
                $string = isset ($myTV) ? $myTV : "";
                $new_string = preg_replace("/[^a-zA-Z0-9s]/", " ", $string);
                return $new_string;
                ?>
                
                  Precision Web Development ... SmashStack.com
                  • 29181
                  • 480 Posts
                  Using return will (I think) return the value and forget anything else in teh function...thus the actual stripping isn’t even getting called:

                  <?php
                  $string = isset ($myTV) ? $myTV : "";
                  echo "The string is $string";
                  $new_string = preg_replace("/[^a-zA-Z0-9s]/", "", $string);
                  return $new_string;
                  ?>



                  Good luck
                    Adrian Lawley: www.adrianlawley.com
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    If you use the construct
                    return function_name();
                    what you’ll usually get returned is the return value of the function call, usually 1 for success or 0 for failure. It will only return the final product of the function if the function is coded to return whatever it does. I went crazy for a while over an odd "1" that was being output to my pages, until I realized my "include external file" snippet was using that form,
                    return include(filename);
                    It was including the file as expected, but was returning the 1 for successful operation!
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org