Skip to content

Commit

Permalink
init from web
Browse files Browse the repository at this point in the history
initialize master
  • Loading branch information
mahmoud-eskandari authored Aug 18, 2017
1 parent 50858b5 commit 7640b03
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "eskandari/persianrender",
"type": "library",
"description": "PersianRender,Free persian letter conertor for using in php GD, etc.",
"keywords": ["text","image","gd","render","persian","persianletter"],
"homepage": "http://webafrooz.com",
"license": "GNU",
"authors": [
{
"name": "Mahmoud Eskandari",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.3"
},
"autoload": {
"psr-4": {
"PersianRender\\": "lib/"
}
}
}
182 changes: 182 additions & 0 deletions lib/PersianRender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace PersianRender;
/**
* The GNU License (GNU v 3.0)
*
* Coyright Under GNU 2.0 License.
* Copyright 2017 Webafrooz.com.
* @package PersianRender
* @author Mahmoud Eskandari <[email protected]>
* @copyright 20017 mahmoud eskandari
* @link https://github.com/mahmoud-eskandari/PersianRender
* @see PersianRender
* @version 0.1
*/

class PersianRender
{
/**
* Charachter List
* @var array
*/
private static $N_LIST = [
'آ' => ['', '', ''],
'ا' => ['', '', ''],
'ب' => ['', '', ''],
'پ' => ['', '', ''],
'ت' => ['', '', ''],
'ث' => ['', '', ''],
'ج' => ['', '', ''],
'چ' => ['', '', ''],
'ح' => ['', '', ''],
'خ' => ['', '', ''],
'د' => ['', '', ''],
'ذ' => ['', '', ''],
'ر' => ['', '', ''],
'ز' => ['', '', ''],
'ژ' => ['', '', ''],
'س' => ['', '', ''],
'ش' => ['', '', ''],
'ص' => ['', '', ''],
'ض' => ['', '', 'ﺿ'],
'ط' => ['', '', ''],
'ظ' => ['', '', ''],
'ع' => ['', '', ''],
'غ' => ['', '', ''],
'ف' => ['', '', ''],
'ق' => ['', '', ''],
'ک' => ['', '', ''],
'گ' => ['', '', ''],
'ل' => ['', '', ''],
'م' => ['', '', ''],
'ن' => ['', '', ''],
'و' => ['', '', ''],
'ه' => ['', '', ''],
'ی' => ['', 'ﯿ', ''],
'ك' => ['', '', ''],
'ي' => ['', '', ''],
'أ' => ['', '', ''],
'ؤ' => ['', '', ''],
'إ' => ['', '', ''],
'ئ' => ['', '', ''],
'ة' => ['', '', '']
];

/**
* Render Persian Letter
* @param $str
* @param bool $reverse
* @return string
*/
public static function render($str, $reverse = false)
{
//
$str = str_replace(['ي', "\0"], ['ی', ''], $str);
$str = self::numeric_replace($str);
$str = self::mb_str_split(trim($str));
$out = [];

$i = 0;
while(isset($str[$i])) {
$l = $i - 1;
if(isset($str[$l])) {
$l = $str[$l];
} else {
$l = false;
}
$r = $i + 1;
if(isset($str[$r])) {
$r = $str[$r];
} else {
$r = false;
}
$out[] = self::howChar($l, $str[$i], $r);
$i++;
}

if($reverse) {
$out = array_reverse($out);
}

return implode('', $out);
}

/**
* Replace Numeric Letters
* @param $val
* @param bool $persian_to_latin
* @return mixed
*/
public static function numeric_replace($val, $persian_to_latin = false)
{
$f = array('۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '۰');
$t = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0');

if($persian_to_latin) {
return str_replace($f, $t, $val);
}

return str_replace($t, $f, $val);
}

/**
* Find Charachter Mode (Middle OR END OR FIRST)
* @param $l
* @param $char
* @param $r
* @return bool|string
*/
private static function howChar($l, $char, $r)
{
if(!isset(self::$N_LIST[$char])) {
return '';
}
$Result = 0;
if(
!empty($r) && array_key_exists($r, self::$N_LIST)
&& !empty(self::$N_LIST[$char][2])
&& !empty(self::$N_LIST[$r][0])
) {
$Result += 4;
}
if(
!empty($l)
&& array_key_exists($l, self::$N_LIST)
&& !empty(self::$N_LIST[$char][0])
&& !empty(self::$N_LIST[$l][2])
) {
$Result += 2;
}
if($Result === 6) {
return self::$N_LIST[$char][1];
}
if($Result === 4) {
return self::$N_LIST[$char][2];
}
if($Result === 2) {
return self::$N_LIST[$char][0];
}
return $char;
}

/**
* Split String in utf-8
* @param $string
* @param int $string_length
* @return array
*/
public static function mb_str_split($string, $string_length = 1)
{
if(mb_strlen($string) > $string_length || !$string_length) {
do {
$parts[] = mb_substr($string, 0, $string_length);
$string = mb_substr($string, $string_length);
} while(!empty($string));
} else {
$parts = array($string);
}
return $parts;
}

}

0 comments on commit 7640b03

Please sign in to comment.