Skip to content

Instantly share code, notes, and snippets.

@hossinasaadi
Created September 2, 2019 18:04
Show Gist options
  • Save hossinasaadi/a704395784085ea948e68b2d6ba20ccd to your computer and use it in GitHub Desktop.
Save hossinasaadi/a704395784085ea948e68b2d6ba20ccd to your computer and use it in GitHub Desktop.
str_slug() (Str::slug) for arabic & Persian (farsi) Languages in laravel
<?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