Skip to content

Instantly share code, notes, and snippets.

@boboldehampsink
Last active May 16, 2016 12:56
Show Gist options
  • Select an option

  • Save boboldehampsink/7354431 to your computer and use it in GitHub Desktop.

Select an option

Save boboldehampsink/7354431 to your computer and use it in GitHub Desktop.

Revisions

  1. boboldehampsink revised this gist Nov 7, 2013. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions SlugifyTwigExtension.php
    Original file line number Diff line number Diff line change
    @@ -28,20 +28,20 @@ public function initRuntime(\Twig_Environment $env)
    public function slugify($slug)
    {
    // Remove HTML tags
    $slug = preg_replace('/<(.*?)>/u', '', $slug);

    // Remove inner-word punctuation.
    $slug = preg_replace('/[\'"‘’“”]/u', '', $slug);

    // Make it lowercase
    $slug = mb_strtolower($slug, 'UTF-8');

    // Get the "words". Split on anything that is not a unicode letter or number.
    // Periods are OK too.
    preg_match_all('/[\p{L}\p{N}\.]+/u', $slug, $words);
    $words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
    $slug = implode('-', $words);

    return $slug;
    $slug = preg_replace('/<(.*?)>/u', '', $slug);
    // Remove inner-word punctuation.
    $slug = preg_replace('/[\'"‘’“”]/u', '', $slug);
    // Make it lowercase
    $slug = mb_strtolower($slug, 'UTF-8');
    // Get the "words". Split on anything that is not a unicode letter or number.
    // Periods are OK too.
    preg_match_all('/[\p{L}\p{N}\.]+/u', $slug, $words);
    $words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
    $slug = implode('-', $words);
    return $slug;
    }
    }
  2. boboldehampsink revised this gist Nov 7, 2013. No changes.
  3. boboldehampsink created this gist Nov 7, 2013.
    32 changes: 32 additions & 0 deletions SlugifyPlugin.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?php
    namespace Craft;

    class SlugifyPlugin extends BasePlugin
    {
    public function getName()
    {
    return Craft::t('Slugify Twig Extension');
    }

    public function getVersion()
    {
    return '1.0';
    }

    public function getDeveloper()
    {
    return 'Bob Olde Hampsink';
    }

    public function getDeveloperUrl()
    {
    return 'http://www.itmundi.nl';
    }

    public function hookAddTwigExtension()
    {
    Craft::import('plugins.slugify.twigextensions.SlugifyTwigExtension');

    return new SlugifyTwigExtension();
    }
    }
    47 changes: 47 additions & 0 deletions SlugifyTwigExtension.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?php
    namespace Craft;

    class SlugifyTwigExtension extends \Twig_Extension
    {
    protected $env;

    public function getName()
    {
    return 'Slugify Twig Extension';
    }

    public function getFilters()
    {
    return array('slugify' => new \Twig_Filter_Method($this, 'slugify'));
    }

    public function getFunctions()
    {
    return array('slugify' => new \Twig_Function_Method($this, 'slugify'));
    }

    public function initRuntime(\Twig_Environment $env)
    {
    $this->env = $env;
    }

    public function slugify($slug)
    {
    // Remove HTML tags
    $slug = preg_replace('/<(.*?)>/u', '', $slug);

    // Remove inner-word punctuation.
    $slug = preg_replace('/[\'"‘’“”]/u', '', $slug);

    // Make it lowercase
    $slug = mb_strtolower($slug, 'UTF-8');

    // Get the "words". Split on anything that is not a unicode letter or number.
    // Periods are OK too.
    preg_match_all('/[\p{L}\p{N}\.]+/u', $slug, $words);
    $words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
    $slug = implode('-', $words);

    return $slug;
    }
    }