xfire123 Posted October 20, 2017 Share Posted October 20, 2017 (edited) Hello im trying to search this kind of parse. I have this string in variable: $a = "май 2017"; How to parse to for example: "5 2017" or "5 17" or "5" I can parse it from EN text but with cyrillic text i cant... Please help b.t.w i cant use array to store all cyrillic months because they coming from different script. Edited October 20, 2017 by xfire123 Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/ Share on other sites More sharing options...
requinix Posted October 20, 2017 Share Posted October 20, 2017 Set the locale correctly and strptime or strtotime should work. Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552881 Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 Set the locale correctly and strptime or strtotime should work. This is my code: if $filename = "Май 2017" <?php $dir_open = opendir('./WEBDams/Data/CHQ/HQ/1/'); while(false !== ($filename = readdir($dir_open))){ if($filename != "." && $filename != ".."){ setlocale(LC_TIME, 'bg_BG.UTF-8','bulgarian'); $filenameNOext = basename($filename, '.pdf'); $filenameNOext2 = strftime($filenameNOext); $filenameNOext3 = preg_replace('/\d/', '', $filenameNOext2); $filenameNOext4 = preg_replace('/\s+/', '', $filenameNOext3); $link = "<li class='sort-item' data-event-date='$filenameNOext4'><a href='./WEBDams/Data/CHQ/HQ/1/$filename' ><strong> $filenameNOext г. $filenameNOext3</strong></a></li>"; echo $link; } } closedir($dir_open); Im using it with this script: (function($){ var container = $(".sort-list"); var items = $(".sort-item"); items.each(function() { // Convert the string in 'data-event-date' attribute to a more // standardized date format var BCDate = $(this).attr("data-event-date"); var standardDate = BCDate[0]; standardDate = new Date(standardDate).getTime(); $(this).attr("data-event-date", standardDate); }); items.sort(function(a,b){ a = parseFloat($(a).attr("data-event-date")); b = parseFloat($(b).attr("data-event-date")); return a>b ? -1 : a<b ? 1 : 0; }).each(function(){ container.prepend(this); }); })(jQuery); /* This script sorts your list in descending order... to change it to ascending order change the "less than" operator (<) to "greater than" (>) */ But its not working for some reason... can you give me an example? Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552882 Share on other sites More sharing options...
requinix Posted October 20, 2017 Share Posted October 20, 2017 I can get it to work but only with a lowercase month name. Note that you're calling strptime() incorrectly. How about IntlDateFormatter::parse (which will take me some time to get set up so I can't test it right now)? Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552883 Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 I can get it to work but only with a lowercase month name. Note that you're calling strptime() incorrectly. How about IntlDateFormatter::parse (which will take me some time to get set up so I can't test it right now)? yes it can be with lowercase. I see the link but cant understand the method very well. Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552884 Share on other sites More sharing options...
Solution requinix Posted October 20, 2017 Solution Share Posted October 20, 2017 Try $formatter = new IntlDateFormatter("bg_BG.UTF-8", null, null, null, null, "MMM yyyy"); $dt = $formatter->parse("Май 2017"); echo date("Y-m-d", $dt);It does require you knowing what date format the filename uses, though. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552885 Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 It giving me: Fatal error: Uncaught Error: Class 'IntlDateFormatter' not found in C:\WEB\Apache24\htdocs\inc_req\menu_HQ.php:2 Stack trace: #0 C:\WEB\Apache24\htdocs\HQ.php(20): require() #1 {main} thrown in C:\WEB\Apache24\htdocs\inc_req\menu_HQ.php on line 2 I comment out extension=php_intl.dll restarted service. Still same error. Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552886 Share on other sites More sharing options...
requinix Posted October 20, 2017 Share Posted October 20, 2017 Comment out? No, you need that. Uncomment it and restart. Does var_dump(extension_loaded("intl"));show the extension is loaded? Does Apache's error log say anything useful? Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552889 Share on other sites More sharing options...
xfire123 Posted October 20, 2017 Author Share Posted October 20, 2017 (edited) its say: PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\WEB\\PHP\\ext\\php_intl.dll' - The specified module could not be found.\r\n in Unknown on line 0 I fixed it when i copy all icu*.dll files in Apache bin directory... at last I tested it and works like a charm. Thank you very much. Edited October 20, 2017 by xfire123 Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1552890 Share on other sites More sharing options...
xfire123 Posted October 25, 2017 Author Share Posted October 25, 2017 Hello again. I'm wondering now how to fix this sort from: 10 01 02 03 04 05 06 07 08 09 to: 1 2 3 4 5 6 7 8 9 10 The main problem i think is the "0" infront Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1553010 Share on other sites More sharing options...
xfire123 Posted October 25, 2017 Author Share Posted October 25, 2017 I used "n" instead of "m" in date. But the sorting in js its not working. Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1553011 Share on other sites More sharing options...
Barand Posted October 25, 2017 Share Posted October 25, 2017 The correct sorting of dates requires Y-m-d format (yyyy-mm-dd). If you are sorting within your code and want to display another format then reformat on output, Quote Link to comment https://forums.phpfreaks.com/topic/305408-parse-months-in-cyrillic-letters-to-numbers/#findComment-1553012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.