Assets.php
15 hours ago
Cache.php
3 weeks ago
Date.php
15 hours ago
Debug.php
14 hours ago
FeedReader.php
15 hours ago
HTML.php
15 hours ago
Helper.php
3 weeks ago
JSON.php
3 weeks ago
Nonce.php
3 weeks ago
Notice.php
15 hours ago
Number.php
3 weeks ago
NumberConverter.php
15 hours ago
Param.php
3 weeks ago
Sanitizing.php
15 hours ago
Strip.php
3 weeks ago
Templates.php
3 weeks ago
User.php
3 weeks ago
Validating.php
3 weeks ago
WooCommerce.php
3 weeks ago
WordPress.php
15 hours ago
Number.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | class Number { |
| 6 | /** |
| 7 | * Fix numbers and convert them to Persian digits style |
| 8 | * |
| 9 | * @param string $content |
| 10 | * |
| 11 | * @return string Fixed number |
| 12 | */ |
| 13 | public static function fixNumber( string $content ): string { |
| 14 | return preg_replace_callback( |
| 15 | '~<(script|style|textarea|pre|code)\b[^>]*>.*?</\1>(*SKIP)(*F)|(?:&#\d{2,4};)|(?:[0]?[a-z][\x20-\x3B=\x3F-\x7F]*)|(\d+(?:\.\d+)?)|<\s*[^>]+>~isu', |
| 16 | static function ( $matches ) { |
| 17 | return isset( $matches[2] ) ? self::toPersian( $matches[2] ) : $matches[0]; |
| 18 | }, $content ); |
| 19 | //return preg_replace_callback( '/(?:&#\d{2,4};)|(?:[0]?[a-z][\x20-\x3B=\x3F-\x7F]*)|(\d+[\d]*)|<\s*[^>]+>/i', 'persian_number', $content ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Converts English numbers to Persian numbers in post contents |
| 24 | * |
| 25 | * @param string $content Post content |
| 26 | * |
| 27 | * @return string Formatted content |
| 28 | */ |
| 29 | public static function persianNumber( string $content ): string { |
| 30 | return isset( $content[1] ) ? self::toPersian( $content[1] ) : $content[0]; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Converts English digits to Persian digits |
| 35 | * |
| 36 | * @param string $number Numbers |
| 37 | * |
| 38 | * @return string Formatted numbers |
| 39 | */ |
| 40 | public static function toPersian( string $number ): string { |
| 41 | return str_replace( |
| 42 | range( 0, 9 ), |
| 43 | array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ), |
| 44 | $number |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Converts Persian digits to English digits |
| 50 | * |
| 51 | * @param string $number Numbers |
| 52 | * |
| 53 | * @return string Formatted numbers |
| 54 | */ |
| 55 | public static function toEnglish( string $number ): string { |
| 56 | return str_replace( |
| 57 | array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ), |
| 58 | range( 0, 9 ), |
| 59 | $number |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 |