Last active
September 9, 2018 07:53
-
-
Save mhipo1364/4dc524a3564b079df5e82b24caa9a22d to your computer and use it in GitHub Desktop.
Persian slug trait for PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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