📜 ⬆️ ⬇️

PHP RUtils - a small library for processing Russian text

When working in Python, I often use the Pytils library to work with Russian text, and I really missed this library when working in PHP. Perhaps I was looking badly, but everything I found did not provide such opportunities or did not correspond to my notions about pure code too much.

And so, once I decided to port the library to PHP, and now I want to share it with the people and really hope for help in improving it: I will be happy with tips, bug reports and especially pull-requests. The library is located on GitHub: github.com/Andre-487/php_rutils

UPD from 10.26.2013: now the library is also available through Composer: packagist.org/packages/andre_487/php_rutils
I also want to say thanks to everyone who helped to improve the library and bring it to a stable release.
')

Library features


PHP RUtils - Pytils port in PHP. These are utilities for working with Russian text. Utilities are divided into the following modules (classes):



Code examples


Numeral module

The choice of the plural form

$variants = array( '', //1 '', //2 '' //5 ); $amount = 15; echo $amount, ' ', RUtils::numeral()->choosePlural($amount, $variants); //Result: 15  echo RUtils::numeral()->getPlural(2, $variants); //Result: 2  

Form selection and output in words

 echo RUtils::numeral()->sumString(1234, RUtils::MALE, $variants); //Result:       

The output of the number in words

 $numeral = RUtils::numeral(); echo $numeral->getInWordsInt(100); //Result:  echo $numeral->getInWordsFloat(100.025); //Result:      echo $numeral->getInWords(100.0); //Result:  

Withdraw the amount of money in rubles

 echo RUtils::numeral()->getRubles(100.25); //Result:      


Dt module

Today's date

Parameters are passed as instances of the class \php_rutils\struct\TimeParams , it is also possible to transfer them as an array

 $params = new TimeParams(); $params->date = null; //    $params->format = ' d FY '; $params->monthInflected = true; echo RUtils::dt()->ruStrFTime($params); //Result:  22  2013  

Historical date

Parameters are passed as an array, the fields are the same as in the TimeParams class.
The date is transmitted as a string in a free format. It is also possible to transfer the date as a Unix timestamp or as an instance of a DateTime class.

 $params = array( 'date' => '09-05-1945', 'format' => 'ld FY     - ', 'monthInflected' => true, 'preposition' => true, ); echo RUtils::dt()->ruStrFTime($params); //Result:   9  1945     -  

The time period to a fixed date in the past

The time formats for this function are similar to those for Dt::ruStrFTime .
The $accuracy parameter is responsible for the details of the information.

 $toTime = new \DateTime('05-06-1945'); echo RUtils::dt()->distanceOfTimeInWords($toTime); //Result: 24 976   $toTime = strtotime('05-06-1945'); $fromTime = null; //now $accuracy = 3; //, ,  echo RUtils::dt()->distanceOfTimeInWords($toTime, $fromTime, $accuracy); //Result: 24 976 , 11 , 21   

The time period between fixed dates

 $fromTime = '1988-01-01 11:40'; $toTime = '2088-01-01 12:35'; $accuracy = 3; //, ,  echo RUtils::dt()->distanceOfTimeInWords($toTime, $fromTime, $accuracy); //Result:  36 525 , 0 , 55  


Translit module

 // echo RUtils::translit()->translify(' —   '); //Result: Muha - eto malen'kaya ptichka //  echo RUtils::translit()->detranslify("SCHuka"); //Result:  //    URL'   echo RUtils::translit()->slugify(' —   '); //Result: muha---eto-malenkaya-ptichka 


Typo module

 $text = <<<TEXT ... . .     (   " " № 45)  Weather Forecast (r),      -   +-451F. TEXT; //  echo RUtils::typo()->typography($text); /** * Result: * ... . .     (  « » №45)  Weather Forecast®, *      —   ±451°F. */ //   "extended" echo RUtils::typo()->typography($text, TypoRules::$EXTENDED_RULES); /** * Result: * … . .     (  « » №45)  Weather Forecast®, *      —   ±451 °F. */ //  echo RUtils::typo()->typography($text, array(TypoRules::DASHES, TypoRules::CLEAN_SPACES)); /** * Result: * ... . .     (  " " № 45)  Weather Forecast (r), *      —   +-451F. */ 

Also (it won't be possible to show here) the Typo module places non-breaking spaces (NBSP, THIN NBSP). This module is designed more for plain text, so special HTML characters are not used, UTF-8 characters are used directly.

Details


When I ported Pytils, I tried to implement all the functionality and make the interfaces recognizable, so that other Pytils users could easily use the PHP version. Also, most of the test cases are taken from Pytils tests. I also borrowed many comments and examples from the Pytils repository. But, of course, it was not without “improvements” in connection with my author's vision and the specifics of PHP.

For example, it was decided to change the signature of the Dt :: ruStrFTime method and use a structure instead of a large list of arguments. In Python, there will be no drama, if all of a sudden, out of a huge number of arguments that have default values, only the last one needs to be changed - the named arguments will come to the rescue; but if a similar situation happens in PHP, it will be a real tragedy. And the parameters of the function decently .

Also, since PHP developers often do not like to pass a class object to a function, but they like to pass an array, the ability to transfer all these parameters as an associative array is implemented.

The preferences of PHP developers regarding dates are also taken into account, I tried to make working with them as comfortable as possible. As you know, over time, PHP often works in two formats: free string (02.03.2005, 2005-03-02) and Unix timestamp. The DateTime class is also sometimes used. I decided that functions that work with dates should work with all three of these types. I also added the ability to specify a time zone for the transmitted time.

I wrote detailed comments on all public methods and fields of structures. Despite the fact that the library is aimed at the Russian-speaking audience, I made a difficult choice to disgrace with English-language comments in order to educate myself and library users about the rules of good open source.

All examples given in the article are available in the form of scripts adapted for execution both on the web server and in the console (including the Windows console with its cp866).

At the moment I have chosen version 0.1 for my library, but I hope that there will be users for it who will help develop it further, and it will get to version 1.0 and even, what the hell is not joking, 2.0.

Source: https://habr.com/ru/post/198544/


All Articles