ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[PLUGIN] DashLower - converts spaces in a string to dashes '-' and makes it lowercase (handy for urls)

August 02, 2009 10:15pm

Subscribe [5]
  • #1 / Aug 02, 2009 10:15pm

    KomodoDave

    39 posts

    (I’m developing locally and don’t have a license yet, so can’t post in the plugins section..)

    <?php
    
    $plugin_info = array(
      'pi_name' => 'DashLower',
      'pi_version' => '1.0',
      'pi_author' => 'N David Brown',
      'pi_author_url' => 'http://www.buildingfrombelow.org/',
      'pi_description' => 'Replaces spaces with dashes, and converts a string to lowercase',
      'pi_usage' => DashLower::usage()
      );
    
    class DashLower
    {
      function DashLower()
      {
          global $TMPL;
          $this->return_data = strtolower(str_replace(' ', '-', $TMPL->tagdata));
      }
    
      // ----------------------------------------
      //  Plugin Usage
      // ----------------------------------------
    
      // This function describes how the plugin is used.
      //  Make sure and use output buffering
    
      function usage()
      {
      ob_start();
      ?>
        {exp:dashlower}your_text{/exp:dashlower}
      <?php
      $buffer = ob_get_contents();
      ob_end_clean();
      return $buffer;
      }
    }
    ?>

    Save it as pi.dashlower.php in your plugins dir, then use it like

    {exp:dashlower}{workProject}{/exp:dashlower}
  • #2 / Oct 31, 2009 10:54pm

    [/root.]

    29 posts

    Thanks. Really useful plugin.

  • #3 / Nov 01, 2009 3:54am

    Matthew Pennell

    221 posts

    You’ll also want to remove any non-standard characters from the string as well, if it’s intended for use as URLifying words:

    $this->return_data = str_replace(' ', '-', strtolower(preg_replace("/[^a-zA-Z0-9\s]/", "", $TMPL->tagdata)));
  • #4 / Nov 01, 2009 4:39am

    Sue Crocker

    26054 posts

    Also not everyone uses - as the separator. The default is underscore _.

    if ($PREFS->ini('word_separator') == 'dash')

    Look at mod.moblog.php around 3180.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases