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

    I've been reading through the documentation on modifying TV output and decided to enquire for some insight.

    Essentially I have a TV that the user has to input a 3 digit (max) product code relating to a particular product. The TV is then used at a certain point to form part of a URL link string.

    Depending on the country, the product code for an item may vary, thankfully only a few select items it seems.

    Here is some psedudo code for my scenario:

    [[*productcode]] = userinput;

    if [[*productcode]] contains preceding 0 // e.g. '045' {
    remove preceding 0; // will remove 0 to yield 45
    }
    else
    return [[*productcode]];

    Thanks
      • 6629
      • 60 Posts
      Create a new Snippet and name it removeLeadingZero
      // remove first zero from String (001 => 01)
      $output = preg_replace("/^0/i", "", $input);
      
      // remove all leading zeros from String (001 => 1)
      // $output = preg_replace("/^0+/i", "", $input); 
      
      return $output;
      


      Then you can use it as an output filter on your TV
      [[*productcode:removeLeadingZero]]


        • 3749
        • 24,544 Posts
        If you want to remove *all* leading zeros, this one-line snippet should be faster:

        return (ltrim($input, '0'));
          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