PHP code example of fallahalireza / persian-tools-laravel
1. Go to this page and download the library: Download fallahalireza/persian-tools-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fallahalireza / persian-tools-laravel example snippets
return [
/*
|--------------------------------------------------------------------------
| Register Rules as Laravel Validator Strings
|--------------------------------------------------------------------------
| When enabled, rules like 'ir_national_id', 'persian_alpha', etc.
| can be used as strings in $rules arrays.
*/
'register_rules' => true,
/*
|--------------------------------------------------------------------------
| Accept Persian Digits Globally
|--------------------------------------------------------------------------
| When enabled, Persian numerals (۰-۹) are automatically converted
| to their ASCII equivalents before validation.
*/
'accept_persian_numbers' => false,
];
use FallahAlireza\PersianTools\Rules\PersianAlpha;
use FallahAlireza\PersianTools\Rules\IranianNationalId;
use FallahAlireza\PersianTools\Rules\IranianMobile;
use FallahAlireza\PersianTools\Rules\IranianBankCardNumber;
use FallahAlireza\PersianTools\Rules\IranianPhone;
use FallahAlireza\PersianTools\Rules\IranianLicensePlate;
use FallahAlireza\PersianTools\Enums\MobileFormat;
$request->validate([
'name' => ['
use FallahAlireza\PersianTools\Enums\PersianRule;
$request->validate([
'name' => ['equired', PersianRule::IranianNationalId->value],
]);
use FallahAlireza\PersianTools\Rules\PersianDate;
use FallahAlireza\PersianTools\Rules\PersianDateBetween;
// Default separator is '/' → e.g. 1403/06/31
new PersianDate()
// Custom separator
new PersianDate(separator: '-') // e.g. 1403-06-31
// Accept Persian digits like ۱۴۰۳/۰۶/۳۱
new PersianDate(convertPersianNumbers: true)
// Validate date between two dates (not inclusive)
new PersianDateBetween('1400/01/01', '1403/12/29')
use FallahAlireza\PersianTools\Rules\IranianMobile;
use FallahAlireza\PersianTools\Enums\MobileFormat;
new IranianMobile(format: MobileFormat::All) // any format (default)
new IranianMobile(format: MobileFormat::Zero) // 09xxxxxxxxx only
new IranianMobile(format: MobileFormat::PlusCode) // +98xxxxxxxxx only
new IranianMobile(format: MobileFormat::ZeroCode) // 0098xxxxxxxxx only
new IranianMobile(format: MobileFormat::Code) // 98xxxxxxxxx only
new IranianMobile(format: MobileFormat::Normal) // 9xxxxxxxxx only
use FallahAlireza\PersianTools\Rules\IranianPhone;
new IranianPhone(withAreaCode: true) // 02112345678
new IranianPhone(withAreaCode: true, areaCodeSeparator: '-') // 021-12345678
new IranianPhone(withAreaCode: false) // 12345678 (no area code)
use FallahAlireza\PersianTools\Rules\IranianBankCardNumber;
use FallahAlireza\PersianTools\Rules\IranianIban;
// Detect bank name from card number
$rule = new IranianBankCardNumber();
$bank = $rule->detectBank('6037991234567890');
// Returns: "بانک ملی ایران"
// Accept card with dash separator: 6037-9912-3456-7890
new IranianBankCardNumber(separator: '-')
// IBAN without 'IR' prefix
new IranianIban(withPrefix: false)
// IBAN with 'IR' prefix (default)
new IranianIban(withPrefix: true)
use FallahAlireza\PersianTools\Rules\IranianLicensePlate;
use FallahAlireza\PersianTools\Rules\IranianPostalCode;
// Standard car plate only: e.g. 12الف34567
new IranianLicensePlate()
// Also accept motorcycle plates: e.g. 123456789
new IranianLicensePlate(allowMotorcycle: true)
// Postal code without separator: 1234567890
new IranianPostalCode()
// Postal code with separator: 12345-67890
new IranianPostalCode(separator: '-')
use FallahAlireza\PersianTools\Enums\MobileFormat;
MobileFormat::All // Any format (default)
MobileFormat::Zero // Starting with 0: 09...
MobileFormat::PlusCode // Starting with +98: +989...
MobileFormat::ZeroCode // Starting with 0098: 00989...
MobileFormat::Code // Starting with 98: 989...
MobileFormat::Normal // Starting with 9: 9...
'locale' => 'fa', // or 'en'
app()->setLocale('fa');
return [
/*
|--------------------------------------------------------------------------
| ثبت قوانین به صورت رشته در validator لاراول
|--------------------------------------------------------------------------
| در صورت فعال بودن، میتوانید قوانین مانند 'ir_national_id'، 'persian_alpha'
| و غیره را به صورت رشته در آرایه $rules استفاده کنید.
*/
'register_rules' => true,
/*
|--------------------------------------------------------------------------
| پذیرش اعداد فارسی به صورت سراسری
|--------------------------------------------------------------------------
| در صورت فعال بودن، اعداد فارسی (۰-۹) قبل از اعتبارسنجی
| به معادل ASCII تبدیل میشوند.
*/
'accept_persian_numbers' => false,
];
use FallahAlireza\PersianTools\Rules\PersianAlpha;
use FallahAlireza\PersianTools\Rules\IranianNationalId;
use FallahAlireza\PersianTools\Rules\IranianMobile;
use FallahAlireza\PersianTools\Rules\IranianBankCardNumber;
use FallahAlireza\PersianTools\Rules\IranianPhone;
use FallahAlireza\PersianTools\Rules\IranianLicensePlate;
use FallahAlireza\PersianTools\Enums\MobileFormat;
$request->validate([
'name' => ['
use FallahAlireza\PersianTools\Enums\PersianRule;
$request->validate([
'name' => ['equired', PersianRule::IranianNationalId->value],
]);
use FallahAlireza\PersianTools\Rules\PersianDate;
use FallahAlireza\PersianTools\Rules\PersianDateBetween;
// جداکننده پیشفرض '/' است — مثلاً ۱۴۰۳/۰۶/۳۱
new PersianDate()
// جداکننده سفارشی
new PersianDate(separator: '-') // مثلاً ۱۴۰۳-۰۶-۳۱
// پذیرش اعداد فارسی مانند ۱۴۰۳/۰۶/۳۱
new PersianDate(convertPersianNumbers: true)
// اعتبارسنجی تاریخ بین دو تاریخ (غیر شامل)
new PersianDateBetween('1400/01/01', '1403/12/29')
use FallahAlireza\PersianTools\Rules\IranianMobile;
use FallahAlireza\PersianTools\Enums\MobileFormat;
new IranianMobile(format: MobileFormat::All) // هر فرمتی (پیشفرض)
new IranianMobile(format: MobileFormat::Zero) // فقط 09xxxxxxxxx
new IranianMobile(format: MobileFormat::PlusCode) // فقط +98xxxxxxxxx
new IranianMobile(format: MobileFormat::ZeroCode) // فقط 0098xxxxxxxxx
new IranianMobile(format: MobileFormat::Code) // فقط 98xxxxxxxxx
new IranianMobile(format: MobileFormat::Normal) // فقط 9xxxxxxxxx
use FallahAlireza\PersianTools\Rules\IranianPhone;
new IranianPhone(withAreaCode: true) // 02112345678
new IranianPhone(withAreaCode: true, areaCodeSeparator: '-') // 021-12345678
new IranianPhone(withAreaCode: false) // 12345678 (بدون کد منطقه)
use FallahAlireza\PersianTools\Rules\IranianBankCardNumber;
use FallahAlireza\PersianTools\Rules\IranianIban;
// تشخیص نام بانک از شماره کارت
$rule = new IranianBankCardNumber();
$bank = $rule->detectBank('6037991234567890');
// خروجی: "بانک ملی ایران"
// پذیرش کارت با جداکننده خط تیره: 6037-9912-3456-7890
new IranianBankCardNumber(separator: '-')
// شبا بدون پیشوند 'IR'
new IranianIban(withPrefix: false)
// شبا با پیشوند 'IR' (پیشفرض)
new IranianIban(withPrefix: true)
use FallahAlireza\PersianTools\Rules\IranianLicensePlate;
use FallahAlireza\PersianTools\Rules\IranianPostalCode;
// فقط پلاک اتومبیل: مثلاً ۱۲الف۳۴۵۶۷
new IranianLicensePlate()
// پذیرش پلاک موتورسیکلت هم: مثلاً ۱۲۳۴۵۶۷۸۹
new IranianLicensePlate(allowMotorcycle: true)
// کد پستی بدون جداکننده: 1234567890
new IranianPostalCode()
// کد پستی با جداکننده: 12345-67890
new IranianPostalCode(separator: '-')
use FallahAlireza\PersianTools\Enums\MobileFormat;
MobileFormat::All // هر فرمتی (پیشفرض)
MobileFormat::Zero // شروع با 0: 09...
MobileFormat::PlusCode // شروع با +98: +989...
MobileFormat::ZeroCode // شروع با 0098: 00989...
MobileFormat::Code // شروع با 98: 989...
MobileFormat::Normal // شروع با 9: 9...