Created
September 2, 2019 18:04
-
-
Save hossinasaadi/a704395784085ea948e68b2d6ba20ccd to your computer and use it in GitHub Desktop.
str_slug() (Str::slug) for arabic & Persian (farsi) Languages in laravel
This file contains 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 | |
/** | |
* Created by PhpStorm. | |
* User: Hossin Asaadi | |
* Date: 9/2/2019 | |
* Time: 7:43 PM | |
*/ | |
namespace App\Traits; | |
trait Utils | |
{ | |
function make_slug($string, $separator = '-') | |
{ | |
$string = trim($string); | |
$string = mb_strtolower($string, 'UTF-8'); | |
// Make alphanumeric (removes all other characters) | |
// this makes the string safe especially when used as a part of a URL | |
// this keeps latin characters and Persian characters as well | |
$string = preg_replace("/[^a-z0-9_\s-ءاآؤئبپتثجچحخدذرزژسشصضطظعغفقكکگلمنوهی]/u", '', $string); | |
// Remove multiple dashes or whitespaces or underscores | |
$string = preg_replace("/[\s-_]+/", ' ', $string); | |
// Convert whitespaces and underscore to the given separator | |
$string = preg_replace("/[\s_]/", $separator, $string); | |
return $string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment