Skip to content

Instantly share code, notes, and snippets.

@mhipo1364
Last active September 9, 2018 07:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mhipo1364/4dc524a3564b079df5e82b24caa9a22d to your computer and use it in GitHub Desktop.
Persian slug trait for PHP
<?php
namespace App\Traits;
use Illuminate\Support\Str;
/**
* Trait Sluggable
* @package App\Traits
*/
trait Sluggable
{
/**
* @return string
*/
public function getSlugAttribute()
{
return $this->slug($this->{$this->sluggable});
}
/**
* @param $value
*
* @return string
*/
public function slug($value)
{
$separator = $this->separator ?? '-';
$limit = $this->slugLimitWord ?? 100;
$string = strtolower($value);
$string = str_replace('‌', ' ', $string);
$string = Str::words($string, $limit);
$string = mb_ereg_replace('([^آ-ی۰-۹a-z0-9]|-)+', $separator, $string);
$string = strtolower($string);
return trim($string, $separator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment