lithium\util\Inflector::slug PHP Method

slug() public static method

Returns a string with all spaces converted to given replacement and non word characters removed. Maps special characters to ASCII using Inflector::$_transliteration, which can be updated using Inflector::rules().
See also: lithium\util\Inflector::rules()
public static slug ( string $string, string $replacement = '-' ) : string
$string string An arbitrary string to convert.
$replacement string The replacement to use for spaces.
return string The converted string.
    public static function slug($string, $replacement = '-')
    {
        $map = static::$_transliteration + array('/[^\\w\\s]/' => ' ', '/\\s+/' => $replacement, '/(?<=[a-z])([A-Z])/' => $replacement . '\\1', str_replace(':rep', preg_quote($replacement, '/'), '/^[:rep]+|[:rep]+$/') => '');
        return preg_replace(array_keys($map), array_values($map), $string);
    }

Usage Example

Beispiel #1
0
 /**
  * Get content of file, parse it with lessc and return formatted css
  *
  * @todo allow for css-file name only, and search it in all avail. webroots
  * @param string $file full path to file
  * @param array $options Additional options to control flow of method
  *                       - header - controls, whether to prepend a header
  *                       - cache - controls, whether to cache the result
  *                       - cachePath - Where to cache files, defaults to
  *                                     resources/tmp/cache
  * @return string|boolean generated css, false in case of error
  */
 public static function file($file, array $options = array())
 {
     $defaults = array('header' => true, 'cache' => true, 'cachePath' => Libraries::get(true, 'resources') . '/tmp/cache', 'cacheKey' => Inflector::slug(str_replace(array(LITHIUM_APP_PATH, '.less'), array('', '.css'), $file)));
     $options += $defaults;
     $css_file = $options['cachePath'] . '/' . $options['cacheKey'];
     if (file_exists($css_file) && filemtime($css_file) >= filemtime($file)) {
         return file_get_contents($css_file);
     }
     if (!file_exists($file)) {
         return false;
     }
     try {
         $less = static::_getLess($file);
         $output = $less->parse();
     } catch (Exception $e) {
         $output = "/* less compiler exception: {$e->getMessage()} */";
     }
     if ($options['header']) {
         $output = static::_prependHeader($output);
     }
     if ($options['cache']) {
         file_put_contents($css_file, $output);
     }
     return $output;
 }
All Usage Examples Of lithium\util\Inflector::slug