We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30585
    • 833 Posts
    Here's a quick output modifier that will shorten a Canadian province name into a postal abbreviation.

    Usage examples
    [[+province:shortencaprov]]
    
    [[*pagetitle:shortencaprov]]
    

    Snippet
    Create a new snippet and name it shortencaprov or whatever you like. In it, post the following code:
    <?php
    $ca_provinces = array(
      "Alberta" => "AB",
      "British Columbia" => "BC",
      "Manitoba" => "MB",
      "New Brunswick" => "NB",
      "Newfoundland and Labrador" => "NL",
      "Northwest Territories" => "NT",
      "Nova Scotia" => "NS",
      "Nunavut" => "NU",
      "Ontario" => "ON",
      "Prince Edward Island" => "PE",
      "Quebec" => "QC",
      "Saskatchewan" => "SK",
      "Yukon" => "YT"
      );
    
      $short = str_replace(array_keys($ca_provinces), array_values($ca_provinces), $input);
      return $short;
    


    Enjoy!
      A MODx Fanatic
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Just for information, there is also the strtr() function which sometimes can be more appropriate for string replacement. http://php.net/manual/en/function.strtr.php
        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
        • 30585
        • 833 Posts
        Excellent! Thanks for the tip Susan.
          A MODx Fanatic
          • 3749
          • 24,544 Posts
          You might try this. It might make it faster, or not. The strpos() function is very fast compared to str_replace() and the str_replace won't be performed unless it's needed. The overhead of the foreach loop may outweigh the advantage, but it might be worth a try:

          <?php
          $ca_provinces = array(
            "Alberta" => "AB",
            "British Columbia" => "BC",
            "Manitoba" => "MB",
            "New Brunswick" => "NB",
            "Newfoundland and Labrador" => "NL",
            "Northwest Territories" => "NT",
            "Nova Scotia" => "NS",
            "Nunavut" => "NU",
            "Ontario" => "ON",
            "Prince Edward Island" => "PE",
            "Quebec" => "QC",
            "Saskatchewan" => "SK",
            "Yukon" => "YT"
            );
          
           $process = false;
           foreach($ca_provinces as $long => $short) {
              if (strpos($input, $long) !== false {
                 $process = true;
              }
           }
          
          if ($process) {
            return str_replace(array_keys($ca_provinces), array_values($ca_provinces), $input);
          }
          return $input;


          A *much* faster way, if it's feasible for your use case (probably not), is to do it in a plugin attached to OnDocFormSave. That way it only happens once and doesn't effect page load times at all.

          [update] On second thought, since your text is short and (at least with the province placeholder) you know the replace is necessary. My version would probably be slower.
            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
            • 30585
            • 833 Posts
            Thanks Bobray. Great insight as usual. I must admit, I didn't give this much thought, as I wanted a quick way to format data submitted via a web form. A plugin definitely makes sense, especially when updating resource fields.
              A MODx Fanatic