Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/PersianTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PersianTools\Traits\Commas;
use PersianTools\Traits\GetBankNameFromCardNumber;
use PersianTools\Traits\NationalId;
use PersianTools\Traits\PersianString;
use PersianTools\Traits\PhoneNumber;
use PersianTools\Traits\VerifyCardNumber;

Expand All @@ -19,6 +20,7 @@ class PersianTools
Commas,
PhoneNumber,
NationalId,
PersianString,
VerifyCardNumber,
GetBankNameFromCardNumber;
}
46 changes: 46 additions & 0 deletions src/Traits/PersianString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace PersianTools\Traits;

trait PersianString
{
/**
* Check if string has one or more persian characters
*
* @param $string
* @return bool
*/
static public function hasPersian($string)
{
$persianChs = explode(' ', 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی');
foreach ($persianChs as $ch) {
if (strpos($string, $ch) !== false) {
return true;
}
}
return false;
}



/**
* Check if string is completely persian
*
* @param $string
* @return bool
*/
static public function isPersian($string)
{
// $persianChs = explode(' ', 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی');
// $symbolsChs = explode(' ', '! @ # $ % ٪ ^ & * ( ) - _ + = [ { ] } / . . , < > ، ٌ ٍ ً ُ ِ : ; . ,');
// $string = str_replace($symbolsChs, null, $string);
// $stringChs = str_split(trim($string));
// foreach ($stringChs as $ch) {
// if (array_search($ch, $persianChs) === false) {
// echo $ch;
// return false;
// }
// }
// return true;
}
}
18 changes: 18 additions & 0 deletions tests/PersianStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PersianTools\Tests;

use PersianTools\PersianTools;
use PHPUnit\Framework\TestCase;

class PersianStringTest extends TestCase
{
public function testNationalId()
{
$this->assertEquals(true, PersianTools::hasPersian('سلام i am multilang جمله.'));
$this->assertEquals(true, PersianTools::isPersian('سلام من فقط فارسی هستم.('));

$this->assertEquals(false, PersianTools::hasPersian('hello world, without any persian characters!'));
$this->assertEquals(false, PersianTools::isPersian('hello world, from another verse.'));
}
}