Cocur\Slugify\Slugify::slugify PHP Method

slugify() public method

Returns the slug-version of the string.
public slugify ( string $string, string | array | null $options = null ) : string
$string string String to slugify
$options string | array | null Options
return string Slugified version of the string
    public function slugify($string, $options = null)
    {
        // BC: the second argument used to be the separator
        if (is_string($options)) {
            $separator = $options;
            $options = [];
            $options['separator'] = $separator;
        }
        $options = array_merge($this->options, (array) $options);
        // Add a custom ruleset without touching the default rules
        if (isset($options['ruleset'])) {
            $rules = array_merge($this->rules, $this->provider->getRules($options['ruleset']));
        } else {
            $rules = $this->rules;
        }
        $string = strtr($string, $rules);
        unset($rules);
        if ($options['lowercase']) {
            $string = mb_strtolower($string);
        }
        $string = preg_replace($options['regexp'], $options['separator'], $string);
        return trim($string, $options['separator']);
    }

Usage Example

Esempio n. 1
0
 /**
  * Saved edited page; called via ajax
  * @return string
  */
 public function postSavePage()
 {
     $okay = true;
     $page_id = $_REQUEST['page_id'];
     $page_content = $_REQUEST['thedata'];
     if ($page_id > 0) {
         $page = Page::find($page_id);
     } else {
         $page = new Page();
         $slugify = new Slugify();
         $browser_title = $_REQUEST['browser_title'];
         $page->browser_title = $browser_title;
         $page->slug = $slugify->slugify($browser_title);
         // verify that the slug is not already in the db
         $results = Page::where('slug', '=', $slugify->slugify($browser_title))->get();
         foreach ($results as $result) {
             $okay = false;
         }
     }
     $page->page_content = $page_content;
     if ($okay) {
         $page->save();
         echo "OK";
     } else {
         echo "Browser Title already in use";
     }
 }
All Usage Examples Of Cocur\Slugify\Slugify::slugify