We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36804
    • 19 Posts
    I am writing a snippet for cleansing slugs from user input:

    <?php
    /**
    *
    * usage : [[createSlug? &string=`s/o/m/e/-/s/l/a/s/h/e/s`]]
    *
    */
    $res = $modx->newObject('modResource');
    $res->set('alias', $res->cleanAlias($string));
    $alias = $res->get('alias');
    unset($res);
    return $alias;


    cleanAlias() doesn't strip the forward slashes.
    I could use str_replace() but it would be cleaner to use the $options array.

    However the following code
    $string='s/o/m/e/-/s/l/a/s/h/e/s';
    $options = array('/');
    $res->cleanAlias($string, $options)
    
    doesn't work for me.

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 36572
      • 20 Posts
      Hi,

      I think your options are a bit off smiley

      It should be an array like with system setting values
      $option = array('friendly_alias_restrict_chars_pattern'=>'/[\/\0\x0B\t\n\r\f\a&=+%#<>"~`@\?\[\]\{\}\|\^\'\\\\]/'');
      


      (i changed the pattern but don't know if it will work tongue )
        Developer @ Sterc bureau voor internet & marketing
      • discuss.answer
        • 36804
        • 19 Posts
        yes that works, thanks a lot!
        (one too many apostrophes at the end though: removed below)

        <?php
        /**
        *
        * usage : [[createSlug? &string=`s/o/m/e/-/s/l/a/s/h/e/s`]]
        *
        */
        $options = array('friendly_alias_restrict_chars_pattern'=>'/[\/\0\x0B\t\n\r\f\a&=+%#<>"~`@\?\[\]\{\}\|\^\'\\\\]/');
        
        $res = $modx->newObject('modResource');
        $res->set('alias', $res->cleanAlias($string, $options));
        $alias = $res->get('alias');
        return $alias;