Todaymade\Daux\DauxHelper::slug PHP Method

slug() public static method

Taken from Stringy
public static slug ( string $title ) : string
$title string
return string
    public static function slug($title)
    {
        foreach (static::charsArray() as $key => $value) {
            $title = str_replace($value, $key, $title);
        }
        $title = preg_replace('/[^\\x20-\\x7E]/u', '', $title);
        $separator = '_';
        // Convert all dashes into underscores
        $title = preg_replace('![' . preg_quote('-') . ']+!u', $separator, $title);
        // Remove all characters that are not the separator, letters, numbers, or whitespace.
        $title = preg_replace('![^' . preg_quote($separator) . '\\pL\\pN\\s]+!u', '', $title);
        // Replace all separator characters and whitespace by a single separator
        $title = preg_replace('![' . preg_quote($separator) . '\\s]+!u', $separator, $title);
        return trim($title, $separator);
    }

Usage Example

示例#1
0
 protected function addId(Heading $node)
 {
     // If the node has an ID, no need to generate it
     $attributes = $node->getData('attributes', []);
     if (array_key_exists('id', $attributes) && !empty($attributes['id'])) {
         // TODO :: check for uniqueness
         return $attributes['id'];
     }
     // Well, seems we have to generate an ID
     $walker = $node->walker();
     $inside = [];
     while ($event = $walker->next()) {
         $insideNode = $event->getNode();
         if ($insideNode instanceof Heading) {
             continue;
         }
         $inside[] = $insideNode;
     }
     $text = '';
     foreach ($inside as $other) {
         if ($other instanceof Text) {
             $text .= ' ' . $other->getContent();
         }
     }
     $text = 'page_' . DauxHelper::slug(trim($text));
     // TODO :: check for uniqueness
     $node->data['attributes']['id'] = $text;
 }
All Usage Examples Of Todaymade\Daux\DauxHelper::slug