Skip to content

Instantly share code, notes, and snippets.

@sadegh19b
Created February 21, 2026 09:42
Show Gist options
  • Select an option

  • Save sadegh19b/b5584c673077587e4211209deac6d514 to your computer and use it in GitHub Desktop.

Select an option

Save sadegh19b/b5584c673077587e4211209deac6d514 to your computer and use it in GitHub Desktop.
<?php
namespace App\Helpers;
class SlugHelper
{
public static function generate(
string $text,
string $separator = '-',
bool $convertNumber = true,
bool $useLaravelSlug = false
): string {
if ($convertNumber) {
$text = self::normalizeNumber($text);
}
if ($useLaravelSlug) {
return \Illuminate\Support\Str::slug($text, $separator, null);
}
$text = self::sanitizeText($text);
$text = preg_replace('/[\s\-]+/', $separator, $text);
$text = trim($text, $separator);
$text = mb_strtolower($text, 'UTF-8');
return $text;
}
protected static function sanitizeText(string $text): string
{
return preg_replace('/[^آ-یa-zA-Z0-9۰-۹٠-٩\-_\s]+/u', '', $text);
}
protected static function normalizeNumber($string)
{
$persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
$arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩', 'ك', 'ي'];
$standard = range(0, 9);
array_push($standard, 'ک', 'ی');
return str_replace($persian, $standard, str_replace($arabic, $standard, $string));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment