multibyte_string_functions.lib.php 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657
  1. <?php
  2. /**
  3. * ==============================================================================
  4. * File: multibyte_string_functions.lib.php
  5. * Main API extension library for Dokeos 1.8.6+ LMS
  6. * A common purpose library for supporting multibyte string aware functions.
  7. * License: GNU/GPL version 2 or later (Free Software Foundation)
  8. * @author: Ivan Tcholakov, ivantcholakov@gmail.com
  9. * October 2008 - initial implementation.
  10. * May 2009 - refactoring and minor corrections have been implemented.
  11. * @package dokeos.library
  12. * ==============================================================================
  13. */
  14. /**
  15. * Notes:
  16. *
  17. * 1. For all the functions from this library witn optional encoding
  18. * parameters, the system's encoding is assumed by default, i.e. the
  19. * value that is returned by api_get_setting('platform_charset') or
  20. * the value of the global variable $charset.
  21. *
  22. * 2. In other aspects, most of the functions in this library try to copy
  23. * behaviour of some core PHP functions and some functions from the
  24. * mbstring extension. Mostly they have similar names prefixed with "api_".
  25. * For your convenience, links have been given to the documentation of the
  26. * original PHP functions. Thus, you may exploit on your previous habits.
  27. *
  28. * 3. Why these function have been introduced? Because they are able to
  29. * support more encodings than the original ones. And which is more
  30. * important - they are UTF-8 aware. So, they should be used for strings
  31. * in natural language. For internal system identificators of file names
  32. * which are supposed to contain only English letters you may use the
  33. * original PHP string functions.
  34. *
  35. * 4. This library requires PHP mbstring extension to be activated.
  36. * When encodings to be used are not supported by mbstring, this library
  37. * is able to exploit the PHP iconv extesion, which in this case should
  38. * be activated too.
  39. *
  40. * 5. For improved sorting of multibyte strings the library uses the intl
  41. * php-extension if it is installed.
  42. */
  43. /**
  44. * ----------------------------------------------------------------------------
  45. * A safe way to calculate binary lenght of a string (as number of bytes)
  46. * ----------------------------------------------------------------------------
  47. */
  48. /**
  49. * Calculates binary lenght of a string, as number of bytes, regardless the php-setting mbstring.func_overload.
  50. * This function should work for all multi-byte related changes of PHP5 configuration.
  51. * @param string $string The input string.
  52. * @return int Returns the length of the input string (or binary data) as number of bytes.
  53. */
  54. function api_byte_count($string) {
  55. static $use_mb_strlen;
  56. if (!isset($use_mb_strlen)) {
  57. $use_mb_strlen = MBSTRING_INSTALLED && ((int) ini_get('mbstring.func_overload') & 2);
  58. }
  59. if ($use_mb_strlen) {
  60. return mb_strlen($string, '8bit');
  61. }
  62. return strlen($string);
  63. // For PHP6 this function probably will contain:
  64. //return strlen((binary)$string);
  65. }
  66. /**
  67. * ----------------------------------------------------------------------------
  68. * Multibyte string conversion functions
  69. * ----------------------------------------------------------------------------
  70. */
  71. /**
  72. * Converts character encoding of a given string.
  73. * @param string $string The string being converted.
  74. * @param string $to_encoding The encoding that $string is being converted to.
  75. * @param string $from_encoding (optional) The encoding that $string is being converted from. If it is omited, the platform character set is assumed.
  76. * @return string Returns the converted string.
  77. * This function is aimed at replacing the function mb_convert_encoding() for human-language strings.
  78. * @link http://php.net/manual/en/function.mb-convert-encoding
  79. */
  80. function api_convert_encoding($string, $to_encoding, $from_encoding = null) {
  81. if (empty($from_encoding)) {
  82. $from_encoding = api_mb_internal_encoding();
  83. }
  84. if (api_equal_encodings($to_encoding, $from_encoding)) {
  85. // When conversion is not needed, the string is returned directly, without validation.
  86. return $string;
  87. }
  88. if (api_mb_supports($to_encoding) && api_mb_supports($from_encoding)) {
  89. return @mb_convert_encoding($string, $to_encoding, $from_encoding);
  90. }
  91. elseif (api_iconv_supports($to_encoding) && api_iconv_supports($from_encoding)) {
  92. return @iconv($from_encoding, $to_encoding, $string);
  93. }
  94. // Here the function gives up.
  95. return $string;
  96. }
  97. /**
  98. * Converts a given string into UTF-8 encoded string.
  99. * @param string $string The string being converted.
  100. * @param string $from_encoding (optional) The encoding that $string is being converted from. If it is omited, the platform character set is assumed.
  101. * @return string Returns the converted string.
  102. * This function is aimed at replacing the function utf8_encode() for human-language strings.
  103. * @link http://php.net/manual/en/function.utf8-encode
  104. */
  105. function api_utf8_encode($string, $from_encoding = null) {
  106. if (empty($from_encoding)) {
  107. $from_encoding = api_mb_internal_encoding();
  108. }
  109. if (api_is_utf8($from_encoding)) {
  110. // When conversion is not needed, the string is returned directly, without validation.
  111. return $string;
  112. }
  113. if (api_mb_supports($from_encoding)) {
  114. return @mb_convert_encoding($string, 'UTF-8', $from_encoding);
  115. }
  116. elseif (api_iconv_supports($from_encoding)) {
  117. return @iconv($from_encoding, 'UTF-8', $string);
  118. }
  119. // Here the function gives up.
  120. return $string;
  121. }
  122. /**
  123. * Converts a given string from UTF-8 encoding to a specified encoding.
  124. * @param string $string The string being converted.
  125. * @param string $to_encoding (optional) The encoding that $string is being converted to. If it is omited, the platform character set is assumed.
  126. * @return string Returns the converted string.
  127. * This function is aimed at replacing the function utf8_decode() for human-language strings.
  128. * @link http://php.net/manual/en/function.utf8-decode
  129. */
  130. function api_utf8_decode($string, $to_encoding = null) {
  131. if (empty($to_encoding)) {
  132. $to_encoding = api_mb_internal_encoding();
  133. }
  134. if (api_is_utf8($to_encoding)) {
  135. // When conversion is not needed, the string is returned directly, without validation.
  136. return $string;
  137. }
  138. if (api_mb_supports($to_encoding)) {
  139. return @mb_convert_encoding($string, $to_encoding, 'UTF-8');
  140. }
  141. elseif (api_iconv_supports($to_encoding)) {
  142. return @iconv('UTF-8', $to_encoding, $string);
  143. }
  144. // Here the function gives up.
  145. return $string;
  146. }
  147. /**
  148. * Converts a given string into the system ecoding (or platform character set).
  149. * When $from encoding is omited on UTF-8 platforms then language dependent encoding
  150. * is guessed/assumed. On non-UTF-8 platforms omited $from encoding is assumed as UTF-8.
  151. * When the parameter $check_utf8_validity is true the function checks string's
  152. * UTF-8 validity and decides whether to try to convert it or not.
  153. * This function is useful for problem detection or making workarounds.
  154. * @param string $string The string being converted.
  155. * @param string $from_encoding (optional) The encoding that $string is being converted from. It is guessed when it is omited.
  156. * @param bool $check_utf8_validity (optional) A flag for UTF-8 validity check as condition for making conversion.
  157. * @return string Returns the converted string.
  158. */
  159. function api_to_system_encoding($string, $from_encoding = null, $check_utf8_validity = false) {
  160. $system_encoding = api_get_system_encoding();
  161. if (empty($from_encoding)) {
  162. if (api_is_utf8($system_encoding)) {
  163. $from_encoding = api_get_non_utf8_encoding();
  164. } else {
  165. $from_encoding = 'UTF-8';
  166. }
  167. }
  168. if (api_equal_encodings($system_encoding, $from_encoding)) {
  169. return $string;
  170. }
  171. if ($check_utf8_validity) {
  172. if (api_is_utf8($system_encoding)) {
  173. if (api_is_valid_utf8($string)) {
  174. return $string;
  175. }
  176. }
  177. elseif (api_is_utf8($from_encoding)) {
  178. if (!api_is_valid_utf8($string)) {
  179. return $string;
  180. }
  181. }
  182. }
  183. return api_convert_encoding($string, $system_encoding, $from_encoding);
  184. }
  185. /**
  186. * Converts all applicable characters to HTML entities.
  187. * @param string $string The input string.
  188. * @param int $quote_style (optional) The quote style - ENT_COMPAT (default), ENT_QUOTES, ENT_NOQUOTES.
  189. * @param string $encoding (optional) The encoding (of the input string) used in conversion. If it is omited, the platform character set is assumed.
  190. * @return string Returns the converted string.
  191. * This function is aimed at replacing the function htmlentities() for human-language strings.
  192. * @link http://php.net/manual/en/function.htmlentities
  193. */
  194. function api_htmlentities($string, $quote_style = ENT_COMPAT, $encoding = null) {
  195. if (empty($encoding)) {
  196. $encoding = api_mb_internal_encoding();
  197. }
  198. if (!api_is_utf8($encoding) && api_html_entity_supports($encoding)) {
  199. return htmlentities($string, $quote_style, $encoding);
  200. }
  201. if (!api_is_encoding_supported($encoding)) {
  202. return $string;
  203. }
  204. $string = api_convert_encoding(api_utf8_encode($string, $encoding), 'HTML-ENTITIES', 'UTF-8');
  205. switch($quote_style) {
  206. case ENT_COMPAT:
  207. $string = str_replace("'", '&quot;', $string);
  208. break;
  209. case ENT_QUOTES:
  210. $string = str_replace("'", '&#039;', str_replace('"', '&quot;', $string));
  211. break;
  212. }
  213. return $string;
  214. }
  215. /**
  216. * Convers HTML entities into normal characters.
  217. * @param string $string The input string.
  218. * @param int $quote_style (optional) The quote style - ENT_COMPAT (default), ENT_QUOTES, ENT_NOQUOTES.
  219. * @param string $encoding (optional) The encoding (of the result) used in conversion. If it is omited, the platform character set is assumed.
  220. * @return string Returns the converted string.
  221. * This function is aimed at replacing the function html_entity_decode() for human-language strings.
  222. * @link http://php.net/html_entity_decode
  223. */
  224. function api_html_entity_decode($string, $quote_style = ENT_COMPAT, $encoding = null) {
  225. if (empty($encoding)) {
  226. $encoding = api_mb_internal_encoding();
  227. }
  228. if (!api_is_utf8($encoding) && api_html_entity_supports($encoding)) {
  229. return html_entity_decode($string, $quote_style, $encoding);
  230. }
  231. if (!api_is_encoding_supported($encoding)) {
  232. return $string;
  233. }
  234. return api_utf8_decode(html_entity_decode(api_convert_encoding($string, 'UTF-8', $encoding), $quote_style, 'UTF-8'), $encoding);
  235. }
  236. /**
  237. * This function encodes (conditionally) a given string to UTF-8 if XmlHttp-request has been detected.
  238. * @param string $string The string being converted.
  239. * @param string $from_encoding (optional) The encoding that $string is being converted from. If it is omited, the platform character set is assumed.
  240. * @return string Returns the converted string.
  241. */
  242. function api_xml_http_response_encode($string, $from_encoding = null) {
  243. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  244. return api_convert_encoding($string, 'UTF-8', $from_encoding);
  245. }
  246. return $string;
  247. }
  248. /**
  249. * This function converts a given string to the encoding that filesystem uses for representing file/folder names.
  250. * @param string $string The string being converted.
  251. * @param string $from_encoding (optional) The encoding that $string is being converted from. If it is omited, the platform character set is assumed.
  252. * @return string Returns the converted string.
  253. */
  254. function api_file_system_encode($string, $from_encoding = null) {
  255. if (empty($from_encoding)) {
  256. $from_encoding = api_mb_internal_encoding();
  257. }
  258. return api_convert_encoding($string, api_get_file_system_encoding(), $from_encoding);
  259. }
  260. /**
  261. * This function converts a given string from the encoding that filesystem uses for representing file/folder names.
  262. * @param string $string The string being converted.
  263. * @param string $from_encoding (optional) The encoding that $string is being converted from. If it is omited, the platform character set is assumed.
  264. * @return string Returns the converted string.
  265. */
  266. function api_file_system_decode($string, $to_encoding = null) {
  267. if (empty($to_encoding)) {
  268. $to_encoding = api_mb_internal_encoding();
  269. }
  270. return api_convert_encoding($string, $to_encoding, api_get_file_system_encoding());
  271. }
  272. /**
  273. * ----------------------------------------------------------------------------
  274. * Common multibyte string functions
  275. * ----------------------------------------------------------------------------
  276. */
  277. /**
  278. * This function returns a string or an array with all occurrences of search in subject (ignoring case) replaced with the given replace value.
  279. * @param mixed $search String or array of strings to be found.
  280. * @param mixed $replace String or array of strings used for replacement.
  281. * @param mixed $subject String or array of strings being searced.
  282. * @param int $count (optional) The number of matched and replaced needles will be returned in count, which is passed by reference.
  283. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  284. * @return mixed String or array as a result.
  285. * Notes:
  286. * If $subject is an array, then the search and replace is performed with every entry of subject, the return value is an array.
  287. * If $search and $replace are arrays, then the function takes a value from each array and uses it to do search and replace on subject.
  288. * If $replace has fewer values than search, then an empty string is used for the rest of replacement values.
  289. * If $search is an array and $replace is a string, then this replacement string is used for every value of search.
  290. * This function is aimed at replacing the function str_ireplace() for human-language strings.
  291. * @link http://php.net/manual/en/function.str-ireplace
  292. * TODO: To be revised and to be checked.
  293. */
  294. function api_str_ireplace($search, $replace, $subject, & $count = null, $encoding = null) {
  295. if (empty($encoding)) {
  296. $encoding = api_mb_internal_encoding();
  297. }
  298. if (is_array($subject)) {
  299. foreach ($subject as $key => $val) {
  300. $subject[$key] = api_str_ireplace($search, $replace, $val, $count, $encoding);
  301. }
  302. return $subject;
  303. }
  304. if (is_array($search)) {
  305. foreach (array_keys($search) as $key) {
  306. if (is_array($replace)) {
  307. if (array_key_exists($key, $replace)) {
  308. $subject = api_str_ireplace($search[$key], $replace[$key], $subject, $count, $encoding);
  309. } else {
  310. $subject = api_str_ireplace($search[$key], '', $subject, $count, $encoding);
  311. }
  312. } else {
  313. $subject = api_str_ireplace($search[$key], $replace, $subject, $count, $encoding);
  314. }
  315. }
  316. return $subject;
  317. }
  318. $search = api_strtolower($search, $encoding);
  319. $subject_lower = api_strtolower($subject, $encoding);
  320. $total_matched_strlen = 0;
  321. $i = 0;
  322. while (preg_match(api_add_pcre_unicode_modifier('/(.*?)'.preg_quote($search, '/').'/s', $encoding), $subject_lower, $matches)) {
  323. $matched_strlen = api_strlen($matches[0], $encoding);
  324. $subject_lower = api_substr($subject_lower, $matched_strlen, api_strlen($subject_lower, $encoding), $encoding);
  325. $offset = $total_matched_strlen + api_strlen($matches[1], $encoding) + ($i * (api_strlen($replace, $encoding) - 1));
  326. $subject = api_substr_replace($subject, $replace, $offset, api_strlen($search), $encoding);
  327. $total_matched_strlen += $matched_strlen;
  328. $i++;
  329. }
  330. $count += $i;
  331. return $subject;
  332. }
  333. /**
  334. * Converts a string to an array.
  335. * @param string $string The input string.
  336. * @param int $split_length Maximum character-length of the chunk, one character by default.
  337. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  338. * @return array The result array of chunks with the spcified length.
  339. * Notes:
  340. * If the optional split_length parameter is specified, the returned array will be broken down into chunks
  341. * with each being split_length in length, otherwise each chunk will be one character in length.
  342. * FALSE is returned if split_length is less than 1.
  343. * If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.
  344. * This function is aimed at replacing the function str_split() for human-language strings.
  345. * @link http://php.net/str_split
  346. */
  347. function api_str_split($string, $split_length = 1, $encoding = null) {
  348. if ($split_length < 1) {
  349. return false;
  350. }
  351. if (empty($encoding)) {
  352. $encoding = api_mb_internal_encoding();
  353. }
  354. $result = array();
  355. if (api_mb_supports($encoding)) {
  356. for ($i = 0, $length = @mb_strlen($string, $encoding); $i < $length; $i += $split_length) {
  357. $result[] = @mb_substr($string, $i, $split_length, $encoding);
  358. }
  359. }
  360. elseif (api_iconv_supports($encoding)) {
  361. for ($i = 0, $length = api_strlen($string, $encoding); $i < $length; $i += $split_length) {
  362. $result[] = api_substr($string, $i, $split_length, $encoding);
  363. }
  364. } else {
  365. for ($i = 0, $length = strlen($string); $i < $length; $i += $split_length) {
  366. $result[] = substr($string, $i, $split_length);
  367. }
  368. }
  369. return $result;
  370. }
  371. /**
  372. * Finds position of first occurrence of a string within another, case insensitive.
  373. * @param string $haystack The string from which to get the position of the first occurrence.
  374. * @param string $needle The string to be found.
  375. * @param int $offset The position in $haystack to start searching from. If it is omitted, searching starts from the beginning.
  376. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  377. * @return mixed Returns the numeric position of the first occurrence of $needle in the $haystack, or FALSE if $needle is not found.
  378. * Note: The first character's position is 0, the second character position is 1, and so on.
  379. * This function is aimed at replacing the functions stripos() and mb_stripos() for human-language strings.
  380. * @link http://php.net/manual/en/function.stripos
  381. * @link http://php.net/manual/en/function.mb-stripos
  382. */
  383. function api_stripos($haystack, $needle, $offset = 0, $encoding = null) {
  384. if (empty($encoding)){
  385. $encoding = api_mb_internal_encoding();
  386. }
  387. if (api_mb_supports($encoding)) {
  388. return @mb_stripos($haystack, $needle, $offset, $encoding);
  389. } elseif (api_iconv_supports($encoding)) {
  390. return api_utf8_decode(@mb_stripos(api_utf8_encode($haystack, $encoding), api_utf8_encode($needle, $encoding), $offset, 'UTF-8'), $encoding);
  391. }
  392. return stripos($haystack, $needle, $offset);
  393. }
  394. /**
  395. * Finds first occurrence of a string within another, case insensitive.
  396. * @param string $haystack The string from which to get the first occurrence.
  397. * @param string @needle The string to be found.
  398. * @param bool $part (optional) Determines which portion of $haystack this function returns. The default value is FALSE.
  399. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  400. * @return mixed Returns the portion of $haystack, or FALSE if $needle is not found.
  401. * Notes:
  402. * If $part is set to TRUE, the function returns all of $haystack from the beginning to the first occurrence of $needle.
  403. * If $part is set to FALSE, the function returns all of $haystack from the first occurrence of $needle to the end.
  404. * This function is aimed at replacing the functions stristr() and mb_stristr() for human-language strings.
  405. * @link http://php.net/manual/en/function.stristr
  406. * @link http://php.net/manual/en/function.mb-stristr
  407. */
  408. function api_stristr($haystack, $needle, $part = false, $encoding = null) {
  409. if (empty($encoding)) {
  410. $encoding = api_mb_internal_encoding();
  411. }
  412. if (api_mb_supports($encoding)) {
  413. return @mb_stristr($haystack, $needle, $part, $encoding);
  414. }
  415. elseif (api_iconv_supports($encoding)) {
  416. return api_utf8_decode(@mb_stristr(api_utf8_encode($haystack, $encoding), api_utf8_encode($needle, $encoding), $part, 'UTF-8'));
  417. }
  418. return stristr($haystack, $needle, $part);
  419. }
  420. /**
  421. * Returns length of the input string.
  422. * @param string $string The string which length is to be calculated.
  423. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  424. * @return int Returns the number of characters within the string. A multi-byte character is counted as 1.
  425. * This function is aimed at replacing the functions strlen() and mb_strlen() for human-language strings.
  426. * @link http://php.net/manual/en/function.strlen
  427. * @link http://php.net/manual/en/function.mb-strlen
  428. * Note: When you use strlen() to test for an empty string, you needn't change it to api_strlen().
  429. * For example, in lines like the following:
  430. * if (strlen($string) > 0)
  431. * if (strlen($string) != 0)
  432. * there is no need the original function strlen() to be changed, it works correctly and faster for these cases.
  433. */
  434. function api_strlen($string, $encoding = null) {
  435. if (empty($encoding)) {
  436. $encoding = api_mb_internal_encoding();
  437. }
  438. if (api_mb_supports($encoding)) {
  439. return @mb_strlen($string, $encoding);
  440. }
  441. elseif (api_iconv_supports($encoding)) {
  442. return @iconv_strlen($string, $encoding);
  443. }
  444. return strlen($string);
  445. }
  446. /**
  447. * Finds position of first occurrence of a string within another.
  448. * @param string $haystack The string from which to get the position of the first occurrence.
  449. * @param string $needle The string to be found.
  450. * @param int $offset (optional) The position in $haystack to start searching from. If it is omitted, searching starts from the beginning.
  451. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  452. * @return mixed Returns the numeric position of the first occurrence of $needle in the $haystack, or FALSE if $needle is not found.
  453. * Note: The first character's position is 0, the second character position is 1, and so on.
  454. * This function is aimed at replacing the functions strpos() and mb_strpos() for human-language strings.
  455. * @link http://php.net/manual/en/function.strpos
  456. * @link http://php.net/manual/en/function.mb-strpos
  457. */
  458. function api_strpos($haystack, $needle, $offset = 0, $encoding = null) {
  459. if (empty($encoding)) {
  460. $encoding = api_mb_internal_encoding();
  461. }
  462. if (api_mb_supports($encoding)) {
  463. return @mb_strpos($haystack, $needle, $offset, $encoding);
  464. }
  465. elseif (api_iconv_supports($encoding)) {
  466. return api_utf8_decode(@mb_strpos(api_utf8_encode($haystack, $encoding), api_utf8_encode($needle, $encoding), $offset, 'UTF-8'), $encoding);
  467. }
  468. return strpos($haystack, $needle, $offset);
  469. }
  470. /**
  471. * Finds the last occurrence of a character in a string.
  472. * @param string $haystack The string from which to get the last occurrence.
  473. * @param string $needle The string which first character is to be found.
  474. * @param bool $part (optional) Determines which portion of $haystack this function returns. The default value is FALSE.
  475. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  476. * @return mixed Returns the portion of $haystack, or FALSE if the first character from $needle is not found.
  477. * Notes:
  478. * If $part is set to TRUE, the function returns all of $haystack from the beginning to the first occurrence.
  479. * If $part is set to FALSE, the function returns all of $haystack from the first occurrence to the end.
  480. * This function is aimed at replacing the functions strrchr() and mb_strrchr() for human-language strings.
  481. * @link http://php.net/manual/en/function.strrchr
  482. * @link http://php.net/manual/en/function.mb-strrchr
  483. */
  484. function api_strrchr($haystack, $needle, $part = false, $encoding = null) {
  485. if (empty($encoding)) {
  486. $encoding = api_mb_internal_encoding();
  487. }
  488. if (api_mb_supports($encoding)) {
  489. return @mb_strrchr($haystack, $needle, $part, $encoding);
  490. }
  491. elseif (api_iconv_supports($encoding)) {
  492. return api_utf8_decode(@mb_strrchr(api_utf8_encode($haystack, $encoding), api_utf8_encode($needle, $encoding), $part, 'UTF-8'), $encoding);
  493. }
  494. return strrchr($haystack, $needle);
  495. }
  496. /**
  497. * Reverses a string.
  498. * @param string $string The string to be reversed.
  499. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  500. * @return string Returns the reversed string.
  501. * This function is aimed at replacing the function strrev() for human-language strings.
  502. * @link http://php.net/manual/en/function.strrev
  503. */
  504. function api_strrev($string, $encoding = null) {
  505. if (empty($string)) {
  506. return '';
  507. }
  508. if (empty($encoding)) {
  509. $encoding = api_mb_internal_encoding();
  510. }
  511. $result = '';
  512. for ($i = api_strlen($string, $encoding) - 1; $i > -1; $i--) {
  513. $result .= api_substr($string, $i, 1, $encoding);
  514. }
  515. return $result;
  516. }
  517. /**
  518. * Finds the position of last occurrence of a string in a string.
  519. * @param string $haystack The string from which to get the position of the last occurrence.
  520. * @param string $needle The string to be found.
  521. * @param int $offset (optional) $offset may be specified to begin searching an arbitrary position. Negative values will stop searching at an arbitrary point prior to the end of the string.
  522. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  523. * @return mixed Returns the numeric position of the first occurrence of $needle in the $haystack, or FALSE if $needle is not found.
  524. * Note: The first character's position is 0, the second character position is 1, and so on.
  525. * This function is aimed at replacing the functions strrpos() and mb_strrpos() for human-language strings.
  526. * @link http://php.net/manual/en/function.strrpos
  527. * @link http://php.net/manual/en/function.mb-strrpos
  528. */
  529. function api_strrpos($haystack, $needle, $offset = 0, $encoding = null) {
  530. if (empty($encoding)) {
  531. $encoding = api_mb_internal_encoding();
  532. }
  533. if (api_mb_supports($encoding)) {
  534. return @mb_strrpos($haystack, $needle, $offset, $encoding);
  535. }
  536. elseif (api_iconv_supports($encoding)) {
  537. return api_utf8_decode(@mb_strrpos(api_utf8_encode($haystack, $encoding), api_utf8_encode($needle, $encoding), $offset, 'UTF-8'), $encoding);
  538. }
  539. return strrpos($haystack, $needle, $offset);
  540. }
  541. /**
  542. * Finds first occurrence of a string within another.
  543. * @param string $haystack The string from which to get the first occurrence.
  544. * @param string @needle The string to be found.
  545. * @param bool $part (optional) Determines which portion of $haystack this function returns. The default value is FALSE.
  546. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  547. * @return mixed Returns the portion of $haystack, or FALSE if $needle is not found.
  548. * Notes:
  549. * If $part is set to TRUE, the function returns all of $haystack from the beginning to the first occurrence of $needle.
  550. * If $part is set to FALSE, the function returns all of $haystack from the first occurrence of $needle to the end.
  551. * This function is aimed at replacing the functions strstr() and mb_strstr() for human-language strings.
  552. * @link http://php.net/manual/en/function.strstr
  553. * @link http://php.net/manual/en/function.mb-strstr
  554. */
  555. function api_strstr($haystack, $needle, $part = false, $encoding = null) {
  556. if (empty($encoding)) {
  557. $encoding = api_mb_internal_encoding();
  558. }
  559. if (api_mb_supports($encoding)) {
  560. return @mb_strstr($haystack, $needle, $part, $encoding);
  561. }
  562. elseif (api_iconv_supports($encoding)) {
  563. return api_utf8_decode(@mb_strstr(api_utf8_encode($haystack, $encoding), api_utf8_encode($needle, $encoding), $part, 'UTF-8'), $encoding);
  564. }
  565. return strstr($haystack, $needle, $part);
  566. }
  567. /**
  568. * Makes a string lowercase.
  569. * @param string $string The string being lowercased.
  570. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  571. * @return string Returns the string with all alphabetic characters converted to lowercase.
  572. * This function is aimed at replacing the functions strtolower() and mb_strtolower() for human-language strings.
  573. * @link http://php.net/manual/en/function.strtolower
  574. * @link http://php.net/manual/en/function.mb-strtolower
  575. */
  576. function api_strtolower($string, $encoding = null) {
  577. if (empty($encoding)) {
  578. $encoding = api_mb_internal_encoding();
  579. }
  580. if (api_mb_supports($encoding)) {
  581. return @mb_strtolower($string, $encoding);
  582. }
  583. elseif (api_iconv_supports($encoding)) {
  584. return api_utf8_decode(@mb_strtolower(api_utf8_encode($string, $encoding), 'UTF-8'), $encoding);
  585. }
  586. return strtolower($string);
  587. }
  588. /**
  589. * Makes a string uppercase.
  590. * @param string $string The string being uppercased.
  591. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  592. * @return string Returns the string with all alphabetic characters converted to uppercase.
  593. * This function is aimed at replacing the functions strtoupper() and mb_strtoupper() for human-language strings.
  594. * @link http://php.net/manual/en/function.strtoupper
  595. * @link http://php.net/manual/en/function.mb-strtoupper
  596. */
  597. function api_strtoupper($string, $encoding = null) {
  598. if (empty($encoding)) {
  599. $encoding = api_mb_internal_encoding();
  600. }
  601. if (api_mb_supports($encoding)) {
  602. return @mb_strtoupper($string, $encoding);
  603. }
  604. elseif (api_iconv_supports($encoding)) {
  605. return api_utf8_decode(@mb_strtoupper(api_utf8_encode($string, $encoding), 'UTF-8'), $encoding);
  606. }
  607. return strtoupper($string);
  608. }
  609. /**
  610. * Translates certain characters.
  611. * @param string $string The string being translated.
  612. * @param mixed $from A string that contains the character to be replaced. This parameter can be also an array with pairs of characters 'from' => 'to'.
  613. * @param string $to (optional) A string that contains the replacing characters.
  614. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  615. * @return string Returns a copy of $string, translating all occurrences of each character in $from to the corresponding character in $to.
  616. * This function is aimed at replacing the function strtr() for human-language strings.
  617. * @link http://php.net/manual/en/function.strtr
  618. * TODO: To be revised and tested. Probably this function will be not needed.
  619. */
  620. function api_strtr($string, $from, $to = null, $encoding = null) {
  621. if (empty($string)) {
  622. return '';
  623. }
  624. if (is_array($from)) {
  625. if (empty($from)) {
  626. return $string;
  627. }
  628. $encoding = $to;
  629. if (empty($encoding)){
  630. $encoding = api_mb_internal_encoding();
  631. }
  632. $translator = $from;
  633. } else {
  634. if (empty($from) || empty($to)) {
  635. return $string;
  636. }
  637. if (empty($encoding)) {
  638. $encoding = api_mb_internal_encoding();
  639. }
  640. $translator = array();
  641. $arr_from = api_str_split($from, 1, $encoding);
  642. $arr_to = api_str_split($to, 1, $encoding);
  643. $n = count($arr_from);
  644. $n2 = count($arr_to);
  645. if ($n > $n2) $n = $n2;
  646. for ($i = 0; $i < $n; $i++) {
  647. $translator[$arr_from[$i]] = $arr_to[$i];
  648. }
  649. }
  650. $arr_string = api_str_split($string, 1, $encoding);
  651. $n = count($arr_string);
  652. $result = '';
  653. for ($i = 0; $i < $n; $i++) {
  654. if (is_set($translator[$arr_string[$i]])) {
  655. $result .= $translator[$arr_string[$i]];
  656. } else {
  657. $result .= $arr_string[$i];
  658. }
  659. }
  660. return $result;
  661. }
  662. /**
  663. // Gets part of a string.
  664. * @param string $string The input string.
  665. * @param int $start The first position from which the extracted part begins.
  666. * @param int $length The length in character of the extracted part.
  667. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  668. * @return string Returns the part of the string specified by the start and length parameters.
  669. * Note: First character's position is 0. Second character position is 1, and so on.
  670. * This function is aimed at replacing the functions substr() and mb_substr() for human-language strings.
  671. * @link http://php.net/manual/en/function.substr
  672. * @link http://php.net/manual/en/function.mb-substr
  673. */
  674. function api_substr($string, $start, $length = null, $encoding = null) {
  675. if (empty($encoding)) {
  676. $encoding = api_mb_internal_encoding();
  677. }
  678. // Passing null as $length would mean 0. This behaviour has been corrected here.
  679. if (is_null($length)) {
  680. $length = api_strlen($string, $encoding);
  681. }
  682. if (api_mb_supports($encoding)) {
  683. return @mb_substr($string, $start, $length, $encoding);
  684. }
  685. elseif (api_iconv_supports($encoding)) {
  686. return api_utf8_decode(@mb_substr(api_utf8_encode($string, $encoding), $start, $length, 'UTF-8'), $encoding);
  687. }
  688. return substr($string, $start, $length);
  689. }
  690. /**
  691. * Replaces text within a portion of a string.
  692. * @param string $string The input string.
  693. * @param string $replacement The replacement string.
  694. * @param int $start The position from which replacing will begin.
  695. * Notes:
  696. * If $start is positive, the replacing will begin at the $start'th offset into the string.
  697. * If $start is negative, the replacing will begin at the $start'th character from the end of the string.
  698. * @param int $length (optional) The position where replacing will end.
  699. * Notes:
  700. * If given and is positive, it represents the length of the portion of the string which is to be replaced.
  701. * If it is negative, it represents the number of characters from the end of string at which to stop replacing.
  702. * If it is not given, then it will default to api_strlen($string); i.e. end the replacing at the end of string.
  703. * If $length is zero, then this function will have the effect of inserting replacement into the string at the given start offset.
  704. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  705. * @return string The result string is returned.
  706. * This function is aimed at replacing the function substr_replace() for human-language strings.
  707. * @link http://php.net/manual/function.substr-replace
  708. */
  709. function api_substr_replace($string, $replacement, $start, $length = null, $encoding = null) {
  710. if (empty($encoding)) {
  711. $encoding = api_mb_internal_encoding();
  712. }
  713. if ($length == null) {
  714. return api_substr($string, 0, $start, $encoding) . $replacement;
  715. } else {
  716. if ($length < 0) {
  717. $length = api_strlen($string, $encoding) - $start + $length;
  718. }
  719. return
  720. api_substr($string, 0, $start, $encoding) . $replacement .
  721. api_substr($string, $start + $length, api_strlen($string, $encoding), $encoding);
  722. }
  723. }
  724. /**
  725. * Makes a string's first character uppercase.
  726. * @param string $string The input string.
  727. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  728. * @return string Returns a string with the first character capitalized, if that character is alphabetic.
  729. * This function is aimed at replacing the function ucfirst() for human-language strings.
  730. * @link http://php.net/manual/en/function.ucfirst
  731. */
  732. function api_ucfirst($string, $encoding = null) {
  733. if (empty($encoding)) {
  734. $encoding = api_mb_internal_encoding();
  735. }
  736. return api_strtoupper(api_substr($string, 0, 1, $encoding), $encoding) . api_substr($string, 1, api_strlen($string, $encoding), $encoding);
  737. }
  738. /**
  739. * Uppercases the first character of each word in a string.
  740. * @param string $string The input string.
  741. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  742. * @return string Returns the modified string.
  743. * This function is aimed at replacing the function ucwords() for human-language strings.
  744. * @link http://php.net/manual/en/function.ucwords
  745. */
  746. function api_ucwords($string, $encoding = null) {
  747. if (empty($encoding)) {
  748. $encoding = api_mb_internal_encoding();
  749. }
  750. if (api_mb_supports($encoding)) {
  751. return @mb_convert_case($string, MB_CASE_TITLE, $encoding);
  752. }
  753. elseif (api_iconv_supports($encoding)) {
  754. return api_utf8_decode(@mb_convert_case(api_utf8_encode($string, $encoding), MB_CASE_TITLE, 'UTF-8'), $encoding);
  755. }
  756. return ucwords($string);
  757. }
  758. /**
  759. * ----------------------------------------------------------------------------
  760. * String operations using regular expressions
  761. * ----------------------------------------------------------------------------
  762. */
  763. /**
  764. * Executes a regular expression match with extended multibyte support.
  765. * By default this function uses the platform character set.
  766. * @param string $pattern The regular expression pattern.
  767. * @param string $string The searched string.
  768. * @param array $regs (optional) If specified, by this passed by reference parameter an array containing found match and its substrings is returned.
  769. * @return mixed 1 if match is found, FALSE if not. If $regs has been specified, byte-length of the found match is returned, or FALSE if no match has been found.
  770. * This function is aimed at replacing the functions ereg() and mb_ereg() for human-language strings.
  771. * @link http://php.net/manual/en/function.ereg
  772. * @link http://php.net/manual/en/function.mb-ereg
  773. */
  774. function api_ereg($pattern, $string, & $regs = null) {
  775. $count = func_num_args();
  776. $encoding = api_mb_regex_encoding();
  777. if (api_mb_supports($encoding)) {
  778. if ($count < 3) {
  779. return @mb_ereg($pattern, $string);
  780. } else {
  781. $result = @mb_ereg($pattern, $string, $regs);
  782. return $result;
  783. }
  784. }
  785. elseif (api_iconv_supports($encoding)) {
  786. api_mb_regex_encoding('UTF-8');
  787. if ($count < 3) {
  788. $result = @mb_ereg(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding));
  789. } else {
  790. $result = @mb_ereg(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding), $regs);
  791. $regs = _api_array_utf8_decode($regs, $encoding);
  792. }
  793. api_mb_regex_encoding($encoding);
  794. return $result;
  795. } else {
  796. if ($count < 3) {
  797. return ereg($pattern, $string);
  798. } else {
  799. return ereg($pattern, $string, $regs);
  800. }
  801. }
  802. }
  803. /**
  804. * Scans string for matches to pattern, then replaces the matched text with replacement, with extended multibyte support.
  805. * By default this function uses the platform character set.
  806. * @param string $pattern The regular expression pattern.
  807. * @param string $replacement The replacement text.
  808. * @param string $string The searched string.
  809. * @param string $option (optional) Matching condition.
  810. * If i is specified for the matching condition parameter, the case will be ignored.
  811. * If x is specified, white space will be ignored.
  812. * If m is specified, match will be executed in multiline mode and line break will be included in '.'.
  813. * If p is specified, match will be executed in POSIX mode, line break will be considered as normal character.
  814. * If e is specified, replacement string will be evaluated as PHP expression.
  815. * @return mixed The modified string is returned. If no matches are found within the string, then it will be returned unchanged. FALSE will be returned on error.
  816. * This function is aimed at replacing the functions ereg_replace() and mb_ereg_replace() for human-language strings.
  817. * @link http://php.net/manual/en/function.ereg-replace
  818. * @link http://php.net/manual/en/function.mb-ereg-replace
  819. */
  820. function api_ereg_replace($pattern, $replacement, $string, $option = null) {
  821. $encoding = api_mb_regex_encoding();
  822. if (api_mb_supports($encoding)) {
  823. if (is_null($option)) {
  824. return @mb_ereg_replace($pattern, $replacement, $string);
  825. } else {
  826. return @mb_ereg_replace($pattern, $replacement, $string, $option);
  827. }
  828. }
  829. elseif (api_iconv_supports($encoding)) {
  830. api_mb_regex_encoding('UTF-8');
  831. if (is_null($option)) {
  832. $result = api_utf8_decode(@mb_ereg_replace(api_utf8_encode($pattern, $encoding), api_utf8_encode($replacement, $encoding), api_utf8_encode($string, $encoding)), $encoding);
  833. } else {
  834. $result = api_utf8_decode(@mb_ereg_replace(api_utf8_encode($pattern, $encoding), api_utf8_encode($replacement, $encoding), api_utf8_encode($string, $encoding), $option), $encoding);
  835. }
  836. api_mb_regex_encoding($encoding);
  837. return $result;
  838. } else {
  839. return ereg_replace($pattern, $replacement, $string);
  840. }
  841. }
  842. /**
  843. * Executes a regular expression match, ignoring case, with extended multibyte support.
  844. * By default this function uses the platform character set.
  845. * @param string $pattern The regular expression pattern.
  846. * @param string $string The searched string.
  847. * @param array $regs (optional) If specified, by this passed by reference parameter an array containing found match and its substrings is returned.
  848. * @return mixed 1 if match is found, FALSE if not. If $regs has been specified, byte-length of the found match is returned, or FALSE if no match has been found.
  849. * This function is aimed at replacing the functions eregi() and mb_eregi() for human-language strings.
  850. * @link http://php.net/manual/en/function.eregi
  851. * @link http://php.net/manual/en/function.mb-eregi
  852. */
  853. function api_eregi($pattern, $string, & $regs = null) {
  854. $count = func_num_args();
  855. $encoding = api_mb_regex_encoding();
  856. if (api_mb_supports($encoding)) {
  857. if ($count < 3) {
  858. return @mb_eregi($pattern, $string);
  859. } else {
  860. return @mb_eregi($pattern, $string, $regs);
  861. }
  862. }
  863. elseif (api_iconv_supports($encoding)) {
  864. api_mb_regex_encoding('UTF-8');
  865. if ($count < 3) {
  866. $result = @mb_eregi(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding));
  867. } else {
  868. $result = @mb_eregi(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding), $regs);
  869. $regs = _api_array_utf8_decode($regs, $encoding);
  870. }
  871. api_mb_regex_encoding($encoding);
  872. return $result;
  873. } else {
  874. if ($count < 3) {
  875. return eregi($pattern, $string);
  876. } else {
  877. return eregi($pattern, $string, $regs);
  878. }
  879. }
  880. }
  881. /**
  882. * Scans string for matches to pattern, then replaces the matched text with replacement, ignoring case, with extended multibyte support.
  883. * By default this function uses the platform character set.
  884. * @param string $pattern The regular expression pattern.
  885. * @param string $replacement The replacement text.
  886. * @param string $string The searched string.
  887. * @param string $option (optional) Matching condition.
  888. * If i is specified for the matching condition parameter, the case will be ignored.
  889. * If x is specified, white space will be ignored.
  890. * If m is specified, match will be executed in multiline mode and line break will be included in '.'.
  891. * If p is specified, match will be executed in POSIX mode, line break will be considered as normal character.
  892. * If e is specified, replacement string will be evaluated as PHP expression.
  893. * @return mixed The modified string is returned. If no matches are found within the string, then it will be returned unchanged. FALSE will be returned on error.
  894. * This function is aimed at replacing the functions eregi_replace() and mb_eregi_replace() for human-language strings.
  895. * @link http://php.net/manual/en/function.eregi-replace
  896. * @link http://php.net/manual/en/function.mb-eregi-replace
  897. */
  898. function api_eregi_replace($pattern, $replacement, $string, $option = null) {
  899. $encoding = api_mb_regex_encoding();
  900. if (api_mb_supports($encoding)) {
  901. if (is_null($option)) {
  902. return @mb_eregi_replace($pattern, $replacement, $string);
  903. } else {
  904. return @mb_eregi_replace($pattern, $replacement, $string, $option);
  905. }
  906. }
  907. elseif (api_iconv_supports($encoding)) {
  908. api_mb_regex_encoding('UTF-8');
  909. if (is_null($option)) {
  910. $result = api_utf8_decode(@mb_eregi_replace(api_utf8_encode($pattern, $encoding), api_utf8_encode($replacement, $encoding), api_utf8_encode($string, $encoding)), $encoding);
  911. } else {
  912. $result = api_utf8_decode(@mb_eregi_replace(api_utf8_encode($pattern, $encoding), api_utf8_encode($replacement, $encoding), api_utf8_encode($string, $encoding), $option), $encoding);
  913. }
  914. api_mb_regex_encoding($encoding);
  915. return $result;
  916. } else {
  917. return eregi_replace($pattern, $replacement, $string);
  918. }
  919. }
  920. /**
  921. * Splits a multibyte string using regular expression pattern and returns the result as an array.
  922. * By default this function uses the platform character set.
  923. * @param string $pattern The regular expression pattern.
  924. * @param string $string The string being split.
  925. * @param int $limit (optional) If this optional parameter $limit is specified, the string will be split in $limit elements as maximum.
  926. * @return array The result as an array.
  927. * This function is aimed at replacing the functions split() and mb_split() for human-language strings.
  928. * @link http://php.net/manual/en/function.split
  929. * @link http://php.net/manual/en/function.mb-split
  930. */
  931. function api_split($pattern, $string, $limit = null) {
  932. $encoding = api_mb_regex_encoding();
  933. if (api_mb_supports($encoding)) {
  934. if (is_null($limit)) {
  935. return @mb_split($pattern, $string);
  936. } else {
  937. return @mb_split($pattern, $string, $limit);
  938. }
  939. }
  940. elseif (api_iconv_supports($encoding)) {
  941. api_mb_regex_encoding('UTF-8');
  942. if (is_null($limit)) {
  943. $result = @mb_split(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding));
  944. } else {
  945. $result = @mb_split(api_utf8_encode($pattern, $encoding), api_utf8_encode($string, $encoding), $limit);
  946. }
  947. $result = _api_array_utf8_decode($result, $encoding);
  948. api_mb_regex_encoding($encoding);
  949. return $result;
  950. } else {
  951. if (is_null($limit)) {
  952. return split($pattern, $string);
  953. } else {
  954. return split($pattern, $string, $limit);
  955. }
  956. }
  957. }
  958. /**
  959. * This function adds a unicode modifier (u suffix) to a Perl-compatible regular expression depending on the specified encoding.
  960. * @param string $pcre The Perl-compatible regular expression.
  961. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  962. * @return string Returns the same regular expression wit a suffix 'u' if $encoding is 'UTF-8'.
  963. */
  964. function api_add_pcre_unicode_modifier($pcre, $encoding = null) {
  965. if (empty($encoding)){
  966. $encoding = api_get_system_encoding();
  967. }
  968. return api_is_utf8($encoding) ? $pcre.'u' : $pcre;
  969. }
  970. /**
  971. * ----------------------------------------------------------------------------
  972. * Common sting operations with arrays
  973. * ----------------------------------------------------------------------------
  974. */
  975. /**
  976. * Checks if a value exists in an array, a case insensitive version of in_array() function with extended multibyte support.
  977. * @param mixed $needle The searched value. If needle is a string, the comparison is done in a case-insensitive manner.
  978. * @param array $haystack The array.
  979. * @param bool $strict (optional) If is set to TRUE then the function will also check the types of the $needle in the $haystack. The default value if FALSE.
  980. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  981. * @return bool Returns TRUE if $needle is found in the array, FALSE otherwise.
  982. * @link http://php.net/manual/en/function.in-array.php
  983. */
  984. function api_in_array_nocase($needle, $haystack, $strict = false, $encoding = null) {
  985. if (is_array($needle)) {
  986. foreach ($needle as $item) {
  987. if (api_in_array_nocase($item, $haystack, $strict, $encoding)) return true;
  988. }
  989. return false;
  990. }
  991. if (!is_string($needle)) {
  992. return in_array($needle, $haystack, $strict);
  993. }
  994. $needle = api_strtolower($needle, $encoding);
  995. foreach ($haystack as $item) {
  996. if ($strict && !is_string($item)) {
  997. continue;
  998. }
  999. if (api_strtolower($item, $encoding) == $needle) {
  1000. return true;
  1001. }
  1002. }
  1003. return false;
  1004. }
  1005. // This is a helper callback function for internal purposes.
  1006. function _api_array_utf8_decode($variable, $encoding) {
  1007. if (is_array($variable)) {
  1008. return array_map('_api_array_utf8_decode', $variable, $encoding);
  1009. }
  1010. if (is_string($var)) {
  1011. return api_utf8_decode($variable, $encoding);
  1012. }
  1013. return $variable;
  1014. }
  1015. /**
  1016. * ----------------------------------------------------------------------------
  1017. * String comparison
  1018. * ----------------------------------------------------------------------------
  1019. */
  1020. /**
  1021. * Performs string comparison, case insensitive, language sensitive, with extended multibyte support.
  1022. * @param string $string1 The first string.
  1023. * @param string $string2 The second string.
  1024. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1025. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1026. * @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  1027. * This function is aimed at replacing the function strcasecmp() for human-language strings.
  1028. * @link http://php.net/manual/en/function.strcasecmp
  1029. */
  1030. function api_strcasecmp($string1, $string2, $language = null, $encoding = null) {
  1031. return api_strcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
  1032. }
  1033. /**
  1034. * Performs string comparison, case sensitive, language sensitive, with extended multibyte support.
  1035. * @param string $string1 The first string.
  1036. * @param string $string2 The second string.
  1037. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1038. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1039. * @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  1040. * This function is aimed at replacing the function strcmp() for human-language strings.
  1041. * @link http://php.net/manual/en/function.strcmp.php
  1042. * @link http://php.net/manual/en/collator.compare.php
  1043. */
  1044. function api_strcmp($string1, $string2, $language = null, $encoding = null) {
  1045. if (INTL_INSTALLED) {
  1046. $collator = _api_get_collator($language);
  1047. if (is_object($collator)) {
  1048. $result = collator_compare($collator, api_utf8_encode($string1, $encoding), api_utf8_encode($string2, $encoding));
  1049. return $result === false ? 0 : $result;
  1050. }
  1051. }
  1052. return strcmp($string1, $string2);
  1053. }
  1054. /**
  1055. * Performs string comparison in so called "natural order", case insensitive, language sensitive, with extended multibyte support.
  1056. * @param string $string1 The first string.
  1057. * @param string $string2 The second string.
  1058. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1059. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1060. * @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  1061. * This function is aimed at replacing the function strnatcasecmp() for human-language strings.
  1062. * @link http://php.net/manual/en/function.strnatcasecmp
  1063. */
  1064. function api_strnatcasecmp($string1, $string2, $language = null, $encoding = null) {
  1065. return api_strnatcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
  1066. }
  1067. /**
  1068. * Performs string comparison in so called "natural order", case sensitive, language sensitive, with extended multibyte support.
  1069. * @param string $string1 The first string.
  1070. * @param string $string2 The second string.
  1071. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1072. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1073. * @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
  1074. * This function is aimed at replacing the function strnatcmp() for human-language strings.
  1075. * @link http://php.net/manual/en/function.strnatcmp.php
  1076. * @link http://php.net/manual/en/collator.compare.php
  1077. */
  1078. function api_strnatcmp($string1, $string2, $language = null, $encoding = null) {
  1079. if (INTL_INSTALLED) {
  1080. $collator = _api_get_alpha_numerical_collator($language);
  1081. if (is_object($collator)) {
  1082. $result = collator_compare($collator, api_utf8_encode($string1, $encoding), api_utf8_encode($string2, $encoding));
  1083. return $result === false ? 0 : $result;
  1084. }
  1085. }
  1086. return strnatcmp($string1, $string2);
  1087. }
  1088. // Returns an instance of Collator class (ICU) created for a specified language, for internal use.
  1089. function _api_get_collator($language = null) {
  1090. static $collator = array();
  1091. if (!isset($collator[$language])) {
  1092. $locale = api_get_locale_from_language($language);
  1093. $collator[$language] = collator_create($locale);
  1094. if (is_object($collator[$language])) {
  1095. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  1096. }
  1097. }
  1098. return $collator[$language];
  1099. }
  1100. // Returns an instance of Collator class (ICU) created for a specified language, for internal use.
  1101. // This collator treats substrings of digits as numbers.
  1102. function _api_get_alpha_numerical_collator($language = null) {
  1103. static $collator = array();
  1104. if (!isset($collator[$language])) {
  1105. $locale = api_get_locale_from_language($language);
  1106. $collator[$language] = collator_create($locale);
  1107. if (is_object($collator[$language])) {
  1108. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  1109. collator_set_attribute($collator[$language], Collator::NUMERIC_COLLATION, Collator::ON);
  1110. }
  1111. }
  1112. return $collator[$language];
  1113. }
  1114. /**
  1115. * ----------------------------------------------------------------------------
  1116. * Sorting arrays
  1117. * ----------------------------------------------------------------------------
  1118. */
  1119. /**
  1120. * Sorts an array with maintaining index association, elements will be arranged from the lowest to the highest.
  1121. * @param array $array The input array.
  1122. * @param int $sort_flag (optional) Shows how elements of the array to be compared.
  1123. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1124. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1125. * @return bool Returns TRUE on success, FALSE on error.
  1126. * Note: $sort_flag may have the following values:
  1127. * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
  1128. * SORT_NUMERIC - items will be compared as numbers;
  1129. * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
  1130. * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
  1131. * This function is aimed at replacing the function asort() for sorting human-language strings.
  1132. * @link http://php.net/manual/en/function.asort.php
  1133. * @link http://php.net/manual/en/collator.asort.php
  1134. */
  1135. function api_asort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
  1136. if (INTL_INSTALLED) {
  1137. if (empty($encoding)) {
  1138. $encoding = api_mb_internal_encoding();
  1139. }
  1140. $collator = _api_get_collator($language);
  1141. if (is_object($collator)) {
  1142. if (api_is_utf8($encoding)) {
  1143. $sort_flag = ($sort_flag == SORT_LOCALE_STRING) ? SORT_STRING : $sort_flag;
  1144. return collator_asort($collator, $array, _api_get_collator_sort_flag($sort_flag));
  1145. }
  1146. elseif ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
  1147. global $_api_collator, $_api_encoding;
  1148. $_api_collator = $collator;
  1149. $_api_encoding = $encoding;
  1150. return uasort($array, '_api_cmp');
  1151. }
  1152. }
  1153. }
  1154. return asort($array, $sort_flag);
  1155. }
  1156. /**
  1157. * Sorts an array with maintaining index association, elements will be arranged from the highest to the lowest (in reverse order).
  1158. * @param array $array The input array.
  1159. * @param int $sort_flag (optional) Shows how elements of the array to be compared.
  1160. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1161. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1162. * @return bool Returns TRUE on success, FALSE on error.
  1163. * Note: $sort_flag may have the following values:
  1164. * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
  1165. * SORT_NUMERIC - items will be compared as numbers;
  1166. * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
  1167. * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
  1168. * This function is aimed at replacing the function arsort() for sorting human-language strings.
  1169. * @link http://php.net/manual/en/function.arsort.php
  1170. */
  1171. function api_arsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
  1172. if (INTL_INSTALLED) {
  1173. if (empty($encoding)) {
  1174. $encoding = api_mb_internal_encoding();
  1175. }
  1176. $collator = _api_get_collator($language);
  1177. if (is_object($collator)) {
  1178. if ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
  1179. global $_api_collator, $_api_encoding;
  1180. $_api_collator = $collator;
  1181. $_api_encoding = $encoding;
  1182. return uasort($array, '_api_rcmp');
  1183. }
  1184. }
  1185. }
  1186. return arsort($array, $sort_flag);
  1187. }
  1188. /**
  1189. * Sorts an array using natural order algorithm.
  1190. * @param array $array The input array.
  1191. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1192. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1193. * @return bool Returns TRUE on success, FALSE on error.
  1194. * This function is aimed at replacing the function natsort() for sorting human-language strings.
  1195. * @link http://php.net/manual/en/function.natsort.php
  1196. */
  1197. function api_natsort(&$array, $language = null, $encoding = null) {
  1198. if (INTL_INSTALLED) {
  1199. if (empty($encoding)) {
  1200. $encoding = api_mb_internal_encoding();
  1201. }
  1202. $collator = _api_get_alpha_numerical_collator($language);
  1203. if (is_object($collator)) {
  1204. global $_api_collator, $_api_encoding;
  1205. $_api_collator = $collator;
  1206. $_api_encoding = $encoding;
  1207. return uasort($array, '_api_cmp');
  1208. }
  1209. }
  1210. return natsort($array);
  1211. }
  1212. /**
  1213. * Sorts an array using natural order algorithm in reverse order.
  1214. * @param array $array The input array.
  1215. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1216. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1217. * @return bool Returns TRUE on success, FALSE on error.
  1218. */
  1219. function api_natrsort(&$array, $language = null, $encoding = null) {
  1220. if (INTL_INSTALLED) {
  1221. if (empty($encoding)) {
  1222. $encoding = api_mb_internal_encoding();
  1223. }
  1224. $collator = _api_get_alpha_numerical_collator($language);
  1225. if (is_object($collator)) {
  1226. global $_api_collator, $_api_encoding;
  1227. $_api_collator = $collator;
  1228. $_api_encoding = $encoding;
  1229. return uasort($array, '_api_rcmp');
  1230. }
  1231. }
  1232. return uasort($array, '_api_strnatrcmp');
  1233. }
  1234. /**
  1235. * Sorts an array using natural order algorithm, case-insensitive.
  1236. * @param array $array The input array.
  1237. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1238. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1239. * @return bool Returns TRUE on success, FALSE on error.
  1240. * This function is aimed at replacing the function natcasesort() for sorting human-language strings.
  1241. * @link http://php.net/manual/en/function.natcasesort.php
  1242. */
  1243. function api_natcasesort(&$array, $language = null, $encoding = null) {
  1244. if (INTL_INSTALLED) {
  1245. if (empty($encoding)) {
  1246. $encoding = api_mb_internal_encoding();
  1247. }
  1248. $collator = _api_get_alpha_numerical_collator($language);
  1249. if (is_object($collator)) {
  1250. global $_api_collator, $_api_encoding;
  1251. $_api_collator = $collator;
  1252. $_api_encoding = $encoding;
  1253. return uasort($array, '_api_casecmp');
  1254. }
  1255. }
  1256. return natcasesort($array);
  1257. }
  1258. /**
  1259. * Sorts an array using natural order algorithm, case-insensitive, reverse order.
  1260. * @param array $array The input array.
  1261. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1262. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1263. * @return bool Returns TRUE on success, FALSE on error.
  1264. */
  1265. function api_natcasersort(&$array, $language = null, $encoding = null) {
  1266. if (INTL_INSTALLED) {
  1267. if (empty($encoding)) {
  1268. $encoding = api_mb_internal_encoding();
  1269. }
  1270. $collator = _api_get_alpha_numerical_collator($language);
  1271. if (is_object($collator)) {
  1272. global $_api_collator, $_api_encoding;
  1273. $_api_collator = $collator;
  1274. $_api_encoding = $encoding;
  1275. return uasort($array, '_api_casercmp');
  1276. }
  1277. }
  1278. return uasort($array, '_api_strnatcasercmp');
  1279. }
  1280. /**
  1281. * Sorts an array by keys, elements will be arranged from the lowest key to the highest key.
  1282. * @param array $array The input array.
  1283. * @param int $sort_flag (optional) Shows how keys of the array to be compared.
  1284. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1285. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1286. * @return bool Returns TRUE on success, FALSE on error.
  1287. * Note: $sort_flag may have the following values:
  1288. * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
  1289. * SORT_NUMERIC - keys will be compared as numbers;
  1290. * SORT_STRING - keys will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
  1291. * SORT_LOCALE_STRING - keys will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
  1292. * This function is aimed at replacing the function ksort() for sorting human-language key strings.
  1293. * @link http://php.net/manual/en/function.ksort.php
  1294. */
  1295. function api_ksort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
  1296. if (INTL_INSTALLED) {
  1297. if (empty($encoding)) {
  1298. $encoding = api_mb_internal_encoding();
  1299. }
  1300. $collator = _api_get_collator($language);
  1301. if (is_object($collator)) {
  1302. if ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
  1303. global $_api_collator, $_api_encoding;
  1304. $_api_collator = $collator;
  1305. $_api_encoding = $encoding;
  1306. return uksort($array, '_api_cmp');
  1307. }
  1308. }
  1309. }
  1310. return ksort($array, $sort_flag);
  1311. }
  1312. /**
  1313. * Sorts an array by keys, elements will be arranged from the highest key to the lowest key (in reverse order).
  1314. * @param array $array The input array.
  1315. * @param int $sort_flag (optional) Shows how keys of the array to be compared.
  1316. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1317. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1318. * @return bool Returns TRUE on success, FALSE on error.
  1319. * Note: $sort_flag may have the following values:
  1320. * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
  1321. * SORT_NUMERIC - keys will be compared as numbers;
  1322. * SORT_STRING - keys will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
  1323. * SORT_LOCALE_STRING - keys will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
  1324. * This function is aimed at replacing the function krsort() for sorting human-language key strings.
  1325. * @link http://php.net/manual/en/function.krsort.php
  1326. */
  1327. function api_krsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
  1328. if (INTL_INSTALLED) {
  1329. if (empty($encoding)) {
  1330. $encoding = api_mb_internal_encoding();
  1331. }
  1332. $collator = _api_get_collator($language);
  1333. if (is_object($collator)) {
  1334. if ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
  1335. global $_api_collator, $_api_encoding;
  1336. $_api_collator = $collator;
  1337. $_api_encoding = $encoding;
  1338. return uksort($array, '_api_rcmp');
  1339. }
  1340. }
  1341. }
  1342. return krsort($array, $sort_flag);
  1343. }
  1344. /**
  1345. * Sorts an array by keys using natural order algorithm.
  1346. * @param array $array The input array.
  1347. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1348. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1349. * @return bool Returns TRUE on success, FALSE on error.
  1350. */
  1351. function api_knatsort(&$array, $language = null, $encoding = null) {
  1352. if (INTL_INSTALLED) {
  1353. if (empty($encoding)) {
  1354. $encoding = api_mb_internal_encoding();
  1355. }
  1356. $collator = _api_get_alpha_numerical_collator($language);
  1357. if (is_object($collator)) {
  1358. global $_api_collator, $_api_encoding;
  1359. $_api_collator = $collator;
  1360. $_api_encoding = $encoding;
  1361. return uksort($array, '_api_cmp');
  1362. }
  1363. }
  1364. return uksort($array, 'strnatcmp');
  1365. }
  1366. /**
  1367. * Sorts an array by keys using natural order algorithm in reverse order.
  1368. * @param array $array The input array.
  1369. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1370. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1371. * @return bool Returns TRUE on success, FALSE on error.
  1372. */
  1373. function api_knatrsort(&$array, $language = null, $encoding = null) {
  1374. if (INTL_INSTALLED) {
  1375. if (empty($encoding)) {
  1376. $encoding = api_mb_internal_encoding();
  1377. }
  1378. $collator = _api_get_alpha_numerical_collator($language);
  1379. if (is_object($collator)) {
  1380. global $_api_collator, $_api_encoding;
  1381. $_api_collator = $collator;
  1382. $_api_encoding = $encoding;
  1383. return uksort($array, '_api_rcmp');
  1384. }
  1385. }
  1386. return uksort($array, '_api_strnatrcmp');
  1387. }
  1388. /**
  1389. * Sorts an array by keys using natural order algorithm, case insensitive.
  1390. * @param array $array The input array.
  1391. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1392. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1393. * @return bool Returns TRUE on success, FALSE on error.
  1394. */
  1395. function api_knatcasesort(&$array, $language = null, $encoding = null) {
  1396. if (INTL_INSTALLED) {
  1397. if (empty($encoding)) {
  1398. $encoding = api_mb_internal_encoding();
  1399. }
  1400. $collator = _api_get_alpha_numerical_collator($language);
  1401. if (is_object($collator)) {
  1402. global $_api_collator, $_api_encoding;
  1403. $_api_collator = $collator;
  1404. $_api_encoding = $encoding;
  1405. return uksort($array, '_api_casecmp');
  1406. }
  1407. }
  1408. return uksort($array, 'strnatcasecmp');
  1409. }
  1410. /**
  1411. * Sorts an array by keys using natural order algorithm, case insensitive, reverse order.
  1412. * @param array $array The input array.
  1413. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1414. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1415. * @return bool Returns TRUE on success, FALSE on error.
  1416. */
  1417. function api_knatcasersort(&$array, $language = null, $encoding = null) {
  1418. if (INTL_INSTALLED) {
  1419. if (empty($encoding)) {
  1420. $encoding = api_mb_internal_encoding();
  1421. }
  1422. $collator = _api_get_alpha_numerical_collator($language);
  1423. if (is_object($collator)) {
  1424. global $_api_collator, $_api_encoding;
  1425. $_api_collator = $collator;
  1426. $_api_encoding = $encoding;
  1427. return uksort($array, '_api_casercmp');
  1428. }
  1429. }
  1430. return uksort($array, '_api_strnatcasercmp');
  1431. }
  1432. /**
  1433. * Sorts an array, elements will be arranged from the lowest to the highest.
  1434. * @param array $array The input array.
  1435. * @param int $sort_flag (optional) Shows how elements of the array to be compared.
  1436. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1437. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1438. * @return bool Returns TRUE on success, FALSE on error.
  1439. * Note: $sort_flag may have the following values:
  1440. * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
  1441. * SORT_NUMERIC - items will be compared as numbers;
  1442. * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
  1443. * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
  1444. * This function is aimed at replacing the function sort() for sorting human-language strings.
  1445. * @link http://php.net/manual/en/function.sort.php
  1446. * @link http://php.net/manual/en/collator.sort.php
  1447. */
  1448. function api_sort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
  1449. if (INTL_INSTALLED) {
  1450. if (empty($encoding)) {
  1451. $encoding = api_mb_internal_encoding();
  1452. }
  1453. $collator = _api_get_collator($language);
  1454. if (is_object($collator)) {
  1455. if (api_is_utf8($encoding)) {
  1456. $sort_flag = ($sort_flag == SORT_LOCALE_STRING) ? SORT_STRING : $sort_flag;
  1457. return collator_sort($collator, $array, _api_get_collator_sort_flag($sort_flag));
  1458. }
  1459. elseif ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
  1460. global $_api_collator, $_api_encoding;
  1461. $_api_collator = $collator;
  1462. $_api_encoding = $encoding;
  1463. return usort($array, '_api_cmp');
  1464. }
  1465. }
  1466. }
  1467. return sort($array, $sort_flag);
  1468. }
  1469. /**
  1470. * Sorts an array, elements will be arranged from the highest to the lowest (in reverse order).
  1471. * @param array $array The input array.
  1472. * @param int $sort_flag (optional) Shows how elements of the array to be compared.
  1473. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  1474. * @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  1475. * @return bool Returns TRUE on success, FALSE on error.
  1476. * Note: $sort_flag may have the following values:
  1477. * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
  1478. * SORT_NUMERIC - items will be compared as numbers;
  1479. * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
  1480. * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
  1481. * This function is aimed at replacing the function rsort() for sorting human-language strings.
  1482. * @link http://php.net/manual/en/function.rsort.php
  1483. */
  1484. function api_rsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
  1485. if (INTL_INSTALLED) {
  1486. if (empty($encoding)) {
  1487. $encoding = api_mb_internal_encoding();
  1488. }
  1489. $collator = _api_get_collator($language);
  1490. if (is_object($collator)) {
  1491. if ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
  1492. global $_api_collator, $_api_encoding;
  1493. $_api_collator = $collator;
  1494. $_api_encoding = $encoding;
  1495. return usort($array, '_api_rcmp');
  1496. }
  1497. }
  1498. }
  1499. return rsort($array, $sort_flag);
  1500. }
  1501. // Global variables used by the sorting functions, for internal use.
  1502. $_api_collator = null;
  1503. $_api_encoding = null;
  1504. // A string comparison function that serves sorting functions, for internal use.
  1505. function _api_cmp($string1, $string2) {
  1506. global $_api_collator, $_api_encoding;
  1507. $result = collator_compare($_api_collator, api_utf8_encode($string1, $_api_encoding), api_utf8_encode($string2, $_api_encoding));
  1508. return $result === false ? 0 : $result;
  1509. }
  1510. // A reverse string comparison function that serves sorting functions, for internal use.
  1511. function _api_rcmp($string1, $string2) {
  1512. global $_api_collator, $_api_encoding;
  1513. $result = collator_compare($_api_collator, api_utf8_encode($string2, $_api_encoding), api_utf8_encode($string1, $_api_encoding));
  1514. return $result === false ? 0 : $result;
  1515. }
  1516. // A case-insensitive string comparison function that serves sorting functions, for internal use.
  1517. function _api_casecmp($string1, $string2) {
  1518. global $_api_collator, $_api_encoding;
  1519. $result = collator_compare($_api_collator, api_strtolower(api_utf8_encode($string1, $_api_encoding), 'UTF-8'), api_strtolower(api_utf8_encode($string2, $_api_encoding), 'UTF-8'));
  1520. return $result === false ? 0 : $result;
  1521. }
  1522. // A reverse case-insensitive string comparison function that serves sorting functions, for internal use.
  1523. function _api_casercmp($string1, $string2) {
  1524. global $_api_collator, $_api_encoding;
  1525. $result = collator_compare($_api_collator, api_strtolower(api_utf8_encode($string2, $_api_encoding), 'UTF-8'), api_strtolower(api_utf8_encode($string1, $_api_encoding), 'UTF-8'));
  1526. return $result === false ? 0 : $result;
  1527. }
  1528. // A reverse function from strnatcmp(), for internal use.
  1529. function _api_strnatrcmp($string1, $string2) {
  1530. return strnatcmp($string2, $string1);
  1531. }
  1532. // A reverse function from strnatcasecmp(), for internal use.
  1533. function _api_strnatcasercmp($string1, $string2) {
  1534. return strnatcasecmp($string2, $string1);
  1535. }
  1536. // A fuction that translates sorting flag constants from php core to correspondent constants from intl extension, for internal use.
  1537. function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) {
  1538. switch ($sort_flag) {
  1539. case SORT_STRING:
  1540. case SORT_SORT_LOCALE_STRING:
  1541. return Collator::SORT_STRING;
  1542. case SORT_NUMERIC:
  1543. return Collator::SORT_NUMERIC;
  1544. }
  1545. return Collator::SORT_REGULAR;
  1546. }
  1547. //----------------------------------------------------------------------------
  1548. // Transliteration, converting ANSI and UTF-8 strings to ASCII strings
  1549. //----------------------------------------------------------------------------
  1550. /**
  1551. * Transliterates a string to a plain ASCII string.
  1552. *
  1553. * Example:
  1554. * echo api_transliterate(api_html_entity_decode(
  1555. * '&#1060;&#1105;&#1076;&#1086;&#1088; '.
  1556. * '&#1052;&#1080;&#1093;&#1072;&#1081;&#1083;&#1086;&#1074;&#1080;&#1095; '.
  1557. * '&#1044;&#1086;&#1089;&#1090;&#1086;&#1077;&#1074;&#1082;&#1080;&#1081;',
  1558. * ENT_QUOTES, 'UTF-8'), 'UTF-8');
  1559. * The output should be: Fyodor Mihaylovich Dostoevkiy
  1560. *
  1561. * @param string $string The input string.
  1562. * @param string $unknown (optional) Replacement character for unknown characters and illegal UTF-8 sequences.
  1563. * @param string $from_encoding (optional) The encoding of the input string. If it is omited, the platform character set is assumed.
  1564. * @return string Plain ASCII output.
  1565. *
  1566. * Based on Drupal's module "Transliteration", version 6.x-2.1, 09-JUN-2009:
  1567. * @author Stefan M. Kudwien (smk-ka)
  1568. * @author Daniel F. Kudwien (sun)
  1569. * @link http://drupal.org/project/transliteration
  1570. *
  1571. * See also MediaWiki's UtfNormal.php and CPAN's Text::Unidecode library
  1572. * @link http://www.mediawiki.org
  1573. * @link http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm).
  1574. *
  1575. * Adaptation for the Dokeos 1.8.6.1, 12-JUN-2009:
  1576. * @author Ivan Tcholakov
  1577. */
  1578. function api_transliterate($string, $unknown = '?', $from_encoding = null) {
  1579. static $map = array();
  1580. $string = api_utf8_encode($string, $from_encoding);
  1581. // Screen out some characters that eg won't be allowed in XML.
  1582. $string = preg_replace('/[\x00-\x08\x0b\x0c\x0e-\x1f]/', $unknown, $string);
  1583. // ASCII is always valid NFC!
  1584. // If we're only ever given plain ASCII, we can avoid the overhead
  1585. // of initializing the decomposition tables by skipping out early.
  1586. if (!preg_match('/[\x80-\xff]/', $string)) {
  1587. return $string;
  1588. }
  1589. static $tail_bytes;
  1590. if (!isset($tail_bytes)) {
  1591. // Each UTF-8 head byte is followed by a certain
  1592. // number of tail bytes.
  1593. $tail_bytes = array();
  1594. for ($n = 0; $n < 256; $n++) {
  1595. if ($n < 0xc0) {
  1596. $remaining = 0;
  1597. }
  1598. elseif ($n < 0xe0) {
  1599. $remaining = 1;
  1600. }
  1601. elseif ($n < 0xf0) {
  1602. $remaining = 2;
  1603. }
  1604. elseif ($n < 0xf8) {
  1605. $remaining = 3;
  1606. }
  1607. elseif ($n < 0xfc) {
  1608. $remaining = 4;
  1609. }
  1610. elseif ($n < 0xfe) {
  1611. $remaining = 5;
  1612. } else {
  1613. $remaining = 0;
  1614. }
  1615. $tail_bytes[chr($n)] = $remaining;
  1616. }
  1617. }
  1618. // Chop the text into pure-ASCII and non-ASCII areas;
  1619. // large ASCII parts can be handled much more quickly.
  1620. // Don't chop up Unicode areas for punctuation, though,
  1621. // that wastes energy.
  1622. preg_match_all('/[\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*/', $string, $matches);
  1623. $result = '';
  1624. foreach ($matches[0] as $str) {
  1625. if ($str{0} < "\x80") {
  1626. // ASCII chunk: guaranteed to be valid UTF-8
  1627. // and in normal form C, so skip over it.
  1628. $result .= $str;
  1629. continue;
  1630. }
  1631. // We'll have to examine the chunk byte by byte to ensure
  1632. // that it consists of valid UTF-8 sequences, and to see
  1633. // if any of them might not be normalized.
  1634. //
  1635. // Since PHP is not the fastest language on earth, some of
  1636. // this code is a little ugly with inner loop optimizations.
  1637. $head = '';
  1638. $chunk = api_byte_count($str);
  1639. // Counting down is faster. I'm *so* sorry.
  1640. $len = $chunk + 1;
  1641. for ($i = -1; --$len; ) {
  1642. $c = $str{++$i};
  1643. if ($remaining = $tail_bytes[$c]) {
  1644. // UTF-8 head byte!
  1645. $sequence = $head = $c;
  1646. do {
  1647. // Look for the defined number of tail bytes...
  1648. if (--$len && ($c = $str{++$i}) >= "\x80" && $c < "\xc0") {
  1649. // Legal tail bytes are nice.
  1650. $sequence .= $c;
  1651. } else {
  1652. if ($len == 0) {
  1653. // Premature end of string!
  1654. // Drop a replacement character into output to
  1655. // represent the invalid UTF-8 sequence.
  1656. $result .= $unknown;
  1657. break 2;
  1658. } else {
  1659. // Illegal tail byte; abandon the sequence.
  1660. $result .= $unknown;
  1661. // Back up and reprocess this byte; it may itself
  1662. // be a legal ASCII or UTF-8 sequence head.
  1663. --$i;
  1664. ++$len;
  1665. continue 2;
  1666. }
  1667. }
  1668. } while (--$remaining);
  1669. $n = ord($head);
  1670. if ($n <= 0xdf) {
  1671. $ord = ($n - 192) * 64 + (ord($sequence{1}) - 128);
  1672. }
  1673. else if ($n <= 0xef) {
  1674. $ord = ($n - 224) * 4096 + (ord($sequence{1}) - 128) * 64 + (ord($sequence{2}) - 128);
  1675. }
  1676. else if ($n <= 0xf7) {
  1677. $ord = ($n - 240) * 262144 + (ord($sequence{1}) - 128) * 4096 + (ord($sequence{2}) - 128) * 64 + (ord($sequence{3}) - 128);
  1678. }
  1679. else if ($n <= 0xfb) {
  1680. $ord = ($n - 248) * 16777216 + (ord($sequence{1}) - 128) * 262144 + (ord($sequence{2}) - 128) * 4096 + (ord($sequence{3}) - 128) * 64 + (ord($sequence{4}) - 128);
  1681. }
  1682. else if ($n <= 0xfd) {
  1683. $ord = ($n - 252) * 1073741824 + (ord($sequence{1}) - 128) * 16777216 + (ord($sequence{2}) - 128) * 262144 + (ord($sequence{3}) - 128) * 4096 + (ord($sequence{4}) - 128) * 64 + (ord($sequence{5}) - 128);
  1684. }
  1685. // Lookup and replace a character from the transliteration database.
  1686. $bank = $ord >> 8;
  1687. // Check if we need to load a new bank
  1688. if (!isset($map[$bank])) {
  1689. if (api_get_path(LIBRARY_PATH) == '/lib/') {
  1690. // Include the bank when we are running the installer script.
  1691. $file = 'transliteration_database/' . sprintf('x%02x', $bank) . '.php';
  1692. } else {
  1693. // Include the bank when the system has been already installed, this is the usual way.
  1694. $file = api_get_path(LIBRARY_PATH).'transliteration_database/' . sprintf('x%02x', $bank) . '.php';
  1695. }
  1696. if (file_exists($file)) {
  1697. $map[$bank] = include ($file);
  1698. } else {
  1699. $map[$bank] = array('en' => array());
  1700. }
  1701. }
  1702. $ord = $ord & 255;
  1703. $result .= isset($map[$bank]['en'][$ord]) ? $map[$bank]['en'][$ord] : $unknown;
  1704. $head = '';
  1705. }
  1706. elseif ($c < "\x80") {
  1707. // ASCII byte.
  1708. $result .= $c;
  1709. $head = '';
  1710. }
  1711. elseif ($c < "\xc0") {
  1712. // Illegal tail bytes.
  1713. if ($head == '') {
  1714. $result .= $unknown;
  1715. }
  1716. } else {
  1717. // Miscellaneous freaks.
  1718. $result .= $unknown;
  1719. $head = '';
  1720. }
  1721. }
  1722. }
  1723. return $result;
  1724. }
  1725. /**
  1726. * ----------------------------------------------------------------------------
  1727. * Encoding management functions
  1728. * ----------------------------------------------------------------------------
  1729. */
  1730. /**
  1731. * Returns the most-probably used non-UTF-8 encoding for the given language.
  1732. * @param string $language (optional) The specified language, the default value is the user intrface language.
  1733. * @return string The correspondent encoding to the specified language.
  1734. */
  1735. function api_get_non_utf8_encoding($language = null) {
  1736. if (empty($language)) {
  1737. $language = api_get_interface_language();
  1738. }
  1739. $language = api_refine_language_id($language);
  1740. $encodings = & api_non_utf8_encodings();
  1741. if (is_array($encodings[$language])) {
  1742. if (!empty($encodings[$language][0])) {
  1743. return $encodings[$language][0];
  1744. } else {
  1745. return 'ISO-8859-15';
  1746. }
  1747. } else {
  1748. return 'ISO-8859-15';
  1749. }
  1750. }
  1751. /**
  1752. * Returns a table with non-UTF-8 encodings for all system languages.
  1753. * @return array Returns an array in the form array('language1' => array('encoding1', encoding2', ...), ...)
  1754. * Note: The function api_get_non_utf8_encoding() returns the first encoding from this array that is correspondent to the given language.
  1755. */
  1756. function & api_non_utf8_encodings() {
  1757. // The following list may have some inconsistencies.
  1758. // Place the most used for your language encoding at the first place.
  1759. // If you are adding an encoding, check whether it is supported either by
  1760. // mbstring library, either by iconv library.
  1761. // If you modify this list, please, follow the given syntax exactly.
  1762. // The language names must be stripped of any suffixes, such as _unicode, _corporate, _org, etc.
  1763. static $encodings =
  1764. '
  1765. arabic: WINDOWS-1256, ISO-8859-6;
  1766. asturian: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1767. bosnian: WINDOWS-1250;
  1768. brazilian: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1769. bulgarian: WINDOWS-1251;
  1770. catalan: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1771. croatian: WINDOWS-1250;
  1772. czech: WINDOWS-1250, ISO-8859-2;
  1773. danish: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1774. dari: WINDOWS-1256;
  1775. dutch: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1776. english: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1777. euskera: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1778. esperanto: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1779. finnish: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1780. french: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1781. friulian: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1782. galician: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1783. georgian: GEORGIAN-ACADEMY, GEORGIAN-PS;
  1784. german: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1785. greek: WINDOWS-1253, ISO-8859-7;
  1786. hebrew: ISO-8859-8, WINDOWS-1255;
  1787. hungarian: WINDOWS-1250, ISO-8859-2;
  1788. indonesian: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1789. italian: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1790. japanese: EUC-JP, ISO-2022-JP, Shift-JIS;
  1791. korean: EUC-KR, ISO-2022-KR, CP949;
  1792. latvian: WINDOWS-1257, ISO-8859-13;
  1793. lithuanian: WINDOWS-1257, ISO-8859-13;
  1794. macedonian: WINDOWS-1251;
  1795. malay: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1796. norwegian: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1797. occitan: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1798. pashto: WINDOWS-1256;
  1799. persian: WINDOWS-1256;
  1800. polish: WINDOWS-1250, ISO-8859-2;
  1801. portuguese: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1802. quechua_cusco: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1803. romanian: WINDOWS-1250, ISO-8859-2;
  1804. russian: KOI8-R, WINDOWS-1251;
  1805. serbian: ISO-8859-15, WINDOWS-1252, ISO-8859-1, WINDOWS-1251;
  1806. simpl_chinese: GB2312, WINDOWS-936;
  1807. slovak: WINDOWS-1250, ISO-8859-2;
  1808. slovenian: WINDOWS-1250, ISO-8859-2;
  1809. spanish: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1810. swahili: ISO-8859-1;
  1811. swedish: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1812. thai: WINDOWS-874, ISO-8859-11;
  1813. trad_chinese: BIG-5, EUC-TW;
  1814. turkce: WINDOWS-1254, ISO-8859-9;
  1815. ukrainian: KOI8-U;
  1816. vietnamese: WINDOWS-1258, VISCII, TCVN;
  1817. yoruba: ISO-8859-15, WINDOWS-1252, ISO-8859-1;
  1818. ';
  1819. if (!is_array($encodings)) {
  1820. $table = explode(';', str_replace(' ', '', $encodings));
  1821. $encodings = array();
  1822. foreach ($table as & $row) {
  1823. $row = trim($row);
  1824. if (!empty($row)) {
  1825. $row = explode(':', $row);
  1826. $encodings[$row[0]] = explode(',', strtoupper($row[1]));
  1827. }
  1828. }
  1829. }
  1830. return $encodings;
  1831. }
  1832. /**
  1833. * This function unifies the encoding identificators, so they could be compared.
  1834. * @param string $encoding The specified encoding.
  1835. * @return string Returns the encoding identificator modified in suitable for comparison way.
  1836. */
  1837. function api_refine_encoding_id($encoding) {
  1838. return strtoupper($encoding);
  1839. }
  1840. /**
  1841. * This function checks whether two $encoding are equal (same, equvalent).
  1842. * @param string $encoding1 The first encoding
  1843. * @param string $encoding2 The second encoding
  1844. * @return bool Returns TRUE if the encodings are equal, FALSE otherwise.
  1845. */
  1846. function api_equal_encodings($encoding1, $encoding2) {
  1847. // We have to deal with aliases. This function alone does not solve
  1848. // the problem entirely. And there is no time for this kind of research.
  1849. // At the momemnt, the quick proposition could be:
  1850. return strcmp(api_refine_encoding_id($encoding1), api_refine_encoding_id($encoding2)) == 0 ? true : false;
  1851. }
  1852. /**
  1853. * This function checks whether a given encoding is UTF-8.
  1854. * @param string $encoding The tested encoding.
  1855. * @return bool Returns TRUE if the given encoding id means UTF-8, otherwise returns false.
  1856. */
  1857. function api_is_utf8($encoding) {
  1858. static $result = array();
  1859. if (!isset($result[$encoding])) {
  1860. $result[$encoding] = api_equal_encodings($encoding, 'UTF-8');
  1861. }
  1862. return $result[$encoding];
  1863. }
  1864. /**
  1865. * This function checks whether a given encoding represents (is an alias of) ISO Latin 1 character set.
  1866. * @param string $encoding The tested encoding.
  1867. * @return bool Returns TRUE if the given encoding id means Latin 1 character set, otherwise returns false.
  1868. */
  1869. function api_is_latin1($encoding) {
  1870. static $latin1_encodings = array('ISO-8859-15', 'ISO-8859-1', 'WINDOWS-1252', 'CP1252', 'ISO8859-15', 'ISO8859-1', 'WIN-1252', '1252');
  1871. return in_array(api_refine_encoding_id($encoding), $latin1_encodings);
  1872. }
  1873. /**
  1874. * This function returns the encoding, currently used by the system.
  1875. * @return string The system's encoding.
  1876. * Note: The value of api_get_setting('platform_charset') is tried to be returned first,
  1877. * on the second place the global variable $charset is tried to be returned. If for some
  1878. * reason both attempts fail, 'ISO-8859-15' will be returned.
  1879. */
  1880. function api_get_system_encoding() {
  1881. $system_encoding = api_get_setting('platform_charset');
  1882. if (!empty($system_encoding)) {
  1883. return $system_encoding;
  1884. }
  1885. global $charset;
  1886. return empty($charset) ? 'ISO-8859-15' : $charset;
  1887. }
  1888. /**
  1889. * This function returns the encoding, currently used by the file system.
  1890. * @return string The file system's encoding, it depends on the locale that OS currently uses.
  1891. * @link http://php.net/manual/en/function.setlocale.php
  1892. * Note: For Linux systems, to see all installed locales type in a terminal locale -a
  1893. */
  1894. function api_get_file_system_encoding() {
  1895. static $file_system_encoding;
  1896. if (!isset($file_system_encoding)) {
  1897. $locale = setlocale(LC_CTYPE, '0');
  1898. $seek_pos = strpos($locale, '.');
  1899. if ($seek_pos !== false) {
  1900. $file_system_encoding = substr($locale, $seek_pos + 1);
  1901. if (IS_WINDOWS_OS) {
  1902. $file_system_encoding = 'CP'.$file_system_encoding;
  1903. }
  1904. }
  1905. // Dealing with some aliases.
  1906. $file_system_encoding = str_ireplace('utf8', 'UTF-8', $file_system_encoding);
  1907. $file_system_encoding = preg_replace('/^CP65001$/', 'UTF-8', $file_system_encoding);
  1908. $file_system_encoding = preg_replace('/^CP(125[0-9])$/', 'WINDOWS-\1', $file_system_encoding);
  1909. $file_system_encoding = str_replace('WINDOWS-1252', 'ISO-8859-15', $file_system_encoding);
  1910. if (empty($file_system_encoding)) {
  1911. if (IS_WINDOWS_OS) {
  1912. // Not expected for Windows, this assignment is here just in case.
  1913. $file_system_encoding = api_get_system_encoding();
  1914. } else {
  1915. // For Ububntu and other UTF-8 enabled Linux systems this fits with the default settings.
  1916. $file_system_encoding = 'UTF-8';
  1917. }
  1918. }
  1919. }
  1920. return $file_system_encoding;
  1921. }
  1922. /**
  1923. * Sets all internal default encodings of the multi-byte functions to the given value.
  1924. * @param string $encoding The specified default encoding.
  1925. * @return void
  1926. * Note: This function should be called once the initialization. Please, avoid further manipulation of the internal default encodings.
  1927. */
  1928. function api_set_default_encoding($encoding) {
  1929. api_mb_internal_encoding($encoding);
  1930. api_mb_regex_encoding($encoding);
  1931. api_iconv_set_encoding('iconv_internal_encoding', $encoding);
  1932. }
  1933. /**
  1934. * Sets/Gets internal character encoding of the common string functions within the PHP mbstring extension.
  1935. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  1936. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  1937. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  1938. * @link http://php.net/manual/en/function.mb-internal-encoding
  1939. */
  1940. function api_mb_internal_encoding($encoding = null) {
  1941. static $mb_internal_encoding = null;
  1942. if (empty($encoding)) {
  1943. if (is_null($mb_internal_encoding)) {
  1944. $mb_internal_encoding = @mb_internal_encoding();
  1945. }
  1946. return $mb_internal_encoding;
  1947. }
  1948. $mb_internal_encoding = $encoding;
  1949. if (api_mb_supports($encoding)) {
  1950. return @mb_internal_encoding($encoding);
  1951. }
  1952. return false;
  1953. }
  1954. /**
  1955. * Sets/Gets internal character encoding of the regular expression functions (ereg-like) within the PHP mbstring extension.
  1956. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  1957. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  1958. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  1959. * @link http://php.net/manual/en/function.mb-regex-encoding
  1960. */
  1961. function api_mb_regex_encoding($encoding = null) {
  1962. static $mb_regex_encoding = null;
  1963. if (empty($encoding)) {
  1964. if (is_null($mb_regex_encoding)) {
  1965. $mb_regex_encoding = @mb_regex_encoding();
  1966. }
  1967. return $mb_regex_encoding;
  1968. }
  1969. $mb_regex_encoding = $encoding;
  1970. if (api_mb_supports($encoding)) {
  1971. return @mb_regex_encoding($encoding);
  1972. }
  1973. return false;
  1974. }
  1975. /**
  1976. * Retrieves specified internal encoding configuration variable within the PHP iconv extension.
  1977. * @param string $type The parameter $type could be: 'iconv_internal_encoding', 'iconv_input_encoding', or 'iconv_output_encoding'.
  1978. * @return mixed The function returns the requested encoding or FALSE on error.
  1979. * @link http://php.net/manual/en/function.iconv-get-encoding
  1980. */
  1981. function api_iconv_get_encoding($type) {
  1982. return api_iconv_set_encoding($type);
  1983. }
  1984. /**
  1985. * Sets specified internal encoding configuration variables within the PHP iconv extension.
  1986. * @param string $type The parameter $type could be: 'iconv_internal_encoding', 'iconv_input_encoding', or 'iconv_output_encoding'.
  1987. * @param string $encoding (optional) The desired encoding to be set.
  1988. * @return bool Returns TRUE on success, FALSE on error.
  1989. * Note: This function is used in the global initialization script for setting these three internal encodings to the platform's character set.
  1990. * @link http://php.net/manual/en/function.iconv-set-encoding
  1991. */
  1992. // Sets current setting for character encoding conversion.
  1993. // The parameter $type could be: 'iconv_internal_encoding', 'iconv_input_encoding', or 'iconv_output_encoding'.
  1994. function api_iconv_set_encoding($type, $encoding = null) {
  1995. static $iconv_internal_encoding = null;
  1996. static $iconv_input_encoding = null;
  1997. static $iconv_output_encoding = null;
  1998. if (!ICONV_INSTALLED) {
  1999. return false;
  2000. }
  2001. switch ($type) {
  2002. case 'iconv_internal_encoding':
  2003. if (empty($encoding)) {
  2004. if (is_null($iconv_internal_encoding)) {
  2005. $iconv_internal_encoding = @iconv_get_encoding($type);
  2006. }
  2007. return $iconv_internal_encoding;
  2008. }
  2009. if (api_iconv_supports($encoding)) {
  2010. if(@iconv_set_encoding($type, $encoding)) {
  2011. $iconv_internal_encoding = $encoding;
  2012. return true;
  2013. } else {
  2014. return false;
  2015. }
  2016. } else {
  2017. return false;
  2018. }
  2019. break;
  2020. case 'iconv_input_encoding':
  2021. if (empty($encoding)) {
  2022. if (is_null($iconv_input_encoding)) {
  2023. $iconv_input_encoding = @iconv_get_encoding($type);
  2024. }
  2025. return $iconv_input_encoding;
  2026. }
  2027. if (api_iconv_supports($encoding)) {
  2028. if(@iconv_set_encoding($type, $encoding)) {
  2029. $iconv_input_encoding = $encoding;
  2030. return true;
  2031. } else {
  2032. return false;
  2033. }
  2034. } else {
  2035. return false;
  2036. }
  2037. break;
  2038. case 'iconv_output_encoding':
  2039. if (empty($encoding)) {
  2040. if (is_null($iconv_output_encoding)) {
  2041. $iconv_output_encoding = @iconv_get_encoding($type);
  2042. }
  2043. return $iconv_output_encoding;
  2044. }
  2045. if (api_iconv_supports($encoding)) {
  2046. if(@iconv_set_encoding($type, $encoding)) {
  2047. $iconv_output_encoding = $encoding;
  2048. return true;
  2049. } else {
  2050. return false;
  2051. }
  2052. } else {
  2053. return false;
  2054. }
  2055. break;
  2056. default:
  2057. return false;
  2058. }
  2059. }
  2060. /**
  2061. * Checks whether a specified encoding is supported by this API.
  2062. * @param string $encoding The specified encoding.
  2063. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  2064. */
  2065. function api_is_encoding_supported($encoding) {
  2066. return api_mb_supports($encoding) || api_iconv_supports($encoding);
  2067. }
  2068. /**
  2069. * Checks whether the specified encoding is supported by the PHP mbstring extension.
  2070. * @param string $encoding The specified encoding.
  2071. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  2072. */
  2073. function api_mb_supports($encoding) {
  2074. static $supported = array();
  2075. $encoding = api_refine_encoding_id($encoding);
  2076. if (!isset($supported[$encoding])) {
  2077. $mb_encodings = mb_list_encodings();
  2078. $mb_encodings = array_map('api_refine_encoding_id', $mb_encodings);
  2079. $supported[$encoding] = in_array($encoding, $mb_encodings);
  2080. }
  2081. return $supported[$encoding] ? true : false;
  2082. }
  2083. /**
  2084. * Checks whether the specified encoding is supported by the PHP iconv extension.
  2085. * @param string $encoding The specified encoding.
  2086. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  2087. */
  2088. function api_iconv_supports($encoding) {
  2089. static $supported = array();
  2090. $encoding = api_refine_encoding_id($encoding);
  2091. if (!isset($supported[$encoding])) {
  2092. if (ICONV_INSTALLED) {
  2093. $test_string = '';
  2094. for ($i = 32; $i < 128; $i++) {
  2095. $test_string .= chr($i);
  2096. }
  2097. $supported[$encoding] = (@iconv_strlen($test_string, $encoding)) ? true : false;
  2098. } else {
  2099. $supported[$encoding] = false;
  2100. }
  2101. }
  2102. return $supported[$encoding];
  2103. }
  2104. /**
  2105. * Checks whether the specified encoding is supported by the html-entitiy related functions.
  2106. * @param string $encoding The specified encoding.
  2107. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  2108. */
  2109. function api_html_entity_supports($encoding) {
  2110. static $supported = array();
  2111. $encoding = api_refine_encoding_id($encoding);
  2112. if (!isset($supported[$encoding])) {
  2113. // See http://php.net/manual/en/function.htmlentities.php
  2114. $html_entity_encodings = array(explode(',',
  2115. '
  2116. ISO-8859-1, ISO8859-1,
  2117. ISO-8859-15, ISO8859-15,
  2118. UTF-8,
  2119. cp866, ibm866, 866,
  2120. cp1251, Windows-1251, win-1251, 1251,
  2121. cp1252, Windows-1252, 1252,
  2122. KOI8-R, koi8-ru, koi8r,
  2123. BIG5, 950,
  2124. GB2312, 936,
  2125. BIG5-HKSCS,
  2126. Shift_JIS, SJIS, 932,
  2127. EUC-JP, EUCJP
  2128. '));
  2129. $html_entity_encodings = array_map('trim', $html_entity_encodings);
  2130. $html_entity_encodings = array_map('api_refine_encoding_id', $html_entity_encodings);
  2131. $supported[$encoding] = in_array($encoding, $html_entity_encodings);
  2132. }
  2133. return $supported[$encoding] ? true : false;
  2134. }
  2135. /**
  2136. * ----------------------------------------------------------------------------
  2137. * String validation functions concerning some encodings
  2138. * ----------------------------------------------------------------------------
  2139. */
  2140. /**
  2141. * Checks a string for UTF-8 validity.
  2142. * @param string $string The string to be tested/validated.
  2143. * @return bool Returns TRUE when the tested string is valid UTF-8 one, FALSE othewise.
  2144. * @link http://en.wikipedia.org/wiki/UTF-8
  2145. */
  2146. function api_is_valid_utf8($string) {
  2147. //return @mb_detect_encoding($string, 'UTF-8', true) == 'UTF-8' ? true : false;
  2148. // Ivan Tcholakov, 05-OCT-2008: I do not trust mb_detect_encoding(). I have
  2149. // found a string with a single cyrillic letter (single byte), that is
  2150. // wrongly detected as UTF-8. Possibly, there would be problems with other
  2151. // languages too. An alternative implementation will be used.
  2152. //$len = strlen($string); // mbstring.func_overload might change behaviour of the function strlen().
  2153. // Ivan Tcholakov, 11-JUN-2009: Let us calculate for sure the length in bytes.
  2154. static $use_mb_strlen;
  2155. if (!isset($use_mb_strlen)) {
  2156. $use_mb_strlen = MBSTRING_INSTALLED && ((int) ini_get('mbstring.func_overload') & 2);
  2157. }
  2158. if ($use_mb_strlen) {
  2159. $len = mb_strlen($string, '8bit');
  2160. } else {
  2161. $len = strlen($string);
  2162. }
  2163. // For PHP6 all this fragment probably will contain:
  2164. //$len = strlen((binary)$string);
  2165. $i = 0;
  2166. while ($i < $len) {
  2167. $byte1 = ord($string[$i++]); // Here the current character begins. Its size is
  2168. // determined by the senior bits in the first byte.
  2169. if (($byte1 & 0x80) == 0x00) { // 0xxxxxxx
  2170. // &
  2171. // 10000000
  2172. // --------
  2173. // 00000000
  2174. // This is s valid character and it contains a single byte.
  2175. }
  2176. elseif (($byte1 & 0xE0) == 0xC0) { // 110xxxxx 10xxxxxx
  2177. // & &
  2178. // 11100000 11000000
  2179. // -------- --------
  2180. // 11000000 10000000
  2181. // The character contains two bytes.
  2182. if ($i == $len)
  2183. return false; // Here the string ends unexpectedly.
  2184. if (!((ord($string[$i++]) & 0xC0) == 0x80))
  2185. return false; // Invalid second byte, invalid string.
  2186. }
  2187. elseif(($byte1 & 0xF0) == 0xE0) { // 1110xxxx 10xxxxxx 10xxxxxx
  2188. // & & &
  2189. // 11110000 11000000 11000000
  2190. // -------- -------- --------
  2191. // 11100000 10000000 10000000
  2192. // This is a character of three bytes.
  2193. if ($i == $len) {
  2194. return false; // Unexpected end of the string.
  2195. }
  2196. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2197. return false; // Invalid second byte.
  2198. }
  2199. if ($i == $len) {
  2200. return false; // Unexpected end of the string.
  2201. }
  2202. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2203. return false; // Invalid third byte, invalid string.
  2204. }
  2205. }
  2206. elseif(($byte1 & 0xF8) == 0xF0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  2207. // & & & &
  2208. // 11111000 11000000 11000000 11000000
  2209. // -------- -------- -------- --------
  2210. // 11110000 10000000 10000000 10000000
  2211. // This is a character of four bytes.
  2212. if ($i == $len) {
  2213. return false;
  2214. }
  2215. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2216. return false;
  2217. }
  2218. if ($i == $len) {
  2219. return false;
  2220. }
  2221. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2222. return false;
  2223. }
  2224. if ($i == $len) {
  2225. return false;
  2226. }
  2227. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2228. return false;
  2229. }
  2230. }
  2231. elseif(($byte1 & 0xFC) == 0xF8) { // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  2232. // & & & & &
  2233. // 11111100 11000000 11000000 11000000 11000000
  2234. // -------- -------- -------- -------- --------
  2235. // 11111000 10000000 10000000 10000000 10000000
  2236. // This is a character of five bytes.
  2237. if ($i == $len) {
  2238. return false;
  2239. }
  2240. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2241. return false;
  2242. }
  2243. if ($i == $len) {
  2244. return false;
  2245. }
  2246. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2247. return false;
  2248. }
  2249. if ($i == $len) {
  2250. return false;
  2251. }
  2252. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2253. return false;
  2254. }
  2255. if ($i == $len) {
  2256. return false;
  2257. }
  2258. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2259. return false;
  2260. }
  2261. }
  2262. elseif(($byte1 & 0xFE) == 0xFC) { // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  2263. // & & & & & &
  2264. // 11111110 11000000 11000000 11000000 11000000 11000000
  2265. // -------- -------- -------- -------- -------- --------
  2266. // 11111100 10000000 10000000 10000000 10000000 10000000
  2267. // This is a character of six bytes.
  2268. if ($i == $len) {
  2269. return false;
  2270. }
  2271. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2272. return false;
  2273. }
  2274. if ($i == $len) {
  2275. return false;
  2276. }
  2277. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2278. return false;
  2279. }
  2280. if ($i == $len) {
  2281. return false;
  2282. }
  2283. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2284. return false;
  2285. }
  2286. if ($i == $len) {
  2287. return false;
  2288. }
  2289. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2290. return false;
  2291. }
  2292. if ($i == $len) {
  2293. return false;
  2294. }
  2295. if (!((ord($string[$i++]) & 0xC0) == 0x80)) {
  2296. return false;
  2297. }
  2298. }
  2299. else {
  2300. return false; // In any other case the character is invalid.
  2301. }
  2302. // Here the current character is valid, it
  2303. // matches to some of the cases above.
  2304. // The next character is to be examinated.
  2305. }
  2306. return true; // Empty strings are valid too.
  2307. }
  2308. /**
  2309. * Checks whether a string contains 7-bit ASCII characters only.
  2310. * @param string $string The string to be tested/validated.
  2311. * @return bool Returns TRUE when the tested string contains 7-bit ASCII characters only, FALSE othewise.
  2312. */
  2313. function api_is_valid_ascii($string) {
  2314. return @mb_detect_encoding($string, 'ASCII', true) == 'ASCII' ? true : false;
  2315. }
  2316. /**
  2317. * ----------------------------------------------------------------------------
  2318. * Language management functions
  2319. * ----------------------------------------------------------------------------
  2320. */
  2321. /**
  2322. * Returns a purified language id, without possible suffixes that will disturb language identification in certain cases.
  2323. * @param string $language The input language identificator, for example 'french_unicode'.
  2324. * @param string The same purified or filtered language identificator, for example 'french'.
  2325. */
  2326. function api_refine_language_id($language) {
  2327. static $search = array('_unicode', '_latin', '_corporate', '_org', '_KM');
  2328. return strtolower(str_replace($search, '', $language));
  2329. }
  2330. /**
  2331. * This function check whether a given language can use Latin 1 encoding.
  2332. * @param string $language The checked language.
  2333. * @return bool TRUE if the given language can use Latin 1 encoding (ISO-8859-15, ISO-8859-1, WINDOWS-1252, ...), FALSE otherwise.
  2334. */
  2335. function api_is_latin1_compatible($language) {
  2336. static $latin1_languages;
  2337. if (!isset($latin1_languages)) {
  2338. $latin1_languages = api_get_latin1_compatible_languages();
  2339. }
  2340. $language = api_refine_language_id($language);
  2341. return in_array($language, $latin1_languages);
  2342. }
  2343. /**
  2344. * This function returns an array of those languages that can use Latin 1 encoding.
  2345. * @return array The array of languages that can use Latin 1 encoding (ISO-8859-15, ISO-8859-1, WINDOWS-1252, ...).
  2346. * Note: The returned language identificators are purified, without suffixes.
  2347. */
  2348. function api_get_latin1_compatible_languages() {
  2349. static $latin1_languages;
  2350. if (!isset($latin1_languages)) {
  2351. $latin1_languages = array();
  2352. $encodings = & api_non_utf8_encodings();
  2353. foreach ($encodings as $key => $value) {
  2354. if (api_is_latin1($value[0])) {
  2355. $latin1_languages[] = $key;
  2356. }
  2357. }
  2358. }
  2359. return $latin1_languages;
  2360. }
  2361. /**
  2362. * ----------------------------------------------------------------------------
  2363. * ICU locales (accessible through intl extension).
  2364. * ----------------------------------------------------------------------------
  2365. */
  2366. /**
  2367. * Returns isocode (see api_get_language_isocode()) which is purified accordingly to
  2368. * be used by the php intl extension (ICU library).
  2369. * @param string $language (optional) This is the name of the folder containing translations for the corresponding language.
  2370. * If $language is omitted, interface language is assumed then.
  2371. * @return string The found language locale id or null on error. Examples: bg, en, pt_BR, ...
  2372. */
  2373. function api_get_locale_from_language($language = null) {
  2374. static $locale = array();
  2375. if (!isset($locale[$language])) {
  2376. $locale[$language] = Database::get_language_isocode($language);
  2377. if (!is_null($locale[$language])) {
  2378. $locale[$language] = str_replace('-', '_', $locale[$language]);
  2379. }
  2380. }
  2381. return $locale[$language];
  2382. }
  2383. /**
  2384. * Sets/gets the default internal value of the locale id (for the intl extension, ICU).
  2385. * @param string $locale (optional) The locale id to be set. When it is omitted, the function returns (gets, reads) the default internal value.
  2386. * @return mixed When the function sets the default value, it returns TRUE on success or FALSE on error. Otherwise the function returns as string the current default value.
  2387. */
  2388. function api_set_default_locale($locale = null) {
  2389. static $default_locale = 'en';
  2390. if (!empty($locale)) {
  2391. $default_locale = $locale;
  2392. if (INTL_INSTALLED) {
  2393. return @locale_set_default($locale);
  2394. }
  2395. return true;
  2396. } else {
  2397. if (INTL_INSTALLED) {
  2398. $default_locale = @locale_get_default();
  2399. }
  2400. }
  2401. return $default_locale;
  2402. }
  2403. /**
  2404. * Gets the default internal value of the locale id (for the intl extension, ICU).
  2405. * @return string Returns as string the current default value.
  2406. */
  2407. function api_get_default_locale() {
  2408. return api_set_default_locale();
  2409. }
  2410. //----------------------------------------------------------------------------
  2411. // Multibyte string functions designed to upgrade the PHP5 mbstring extension
  2412. //----------------------------------------------------------------------------
  2413. // ---------- Multibyte string functions implemented in PHP 5.2.0+ -----------
  2414. // This is a multibyte replacement of strchr().
  2415. // This function exists in PHP 5 >= 5.2.0
  2416. // See http://php.net/manual/en/function.mb-strrchr
  2417. if (MBSTRING_INSTALLED && !function_exists('mb_strchr')) {
  2418. function mb_strchr($haystack, $needle, $part = false, $encoding = null) {
  2419. if (empty($encoding)) {
  2420. $encoding = mb_internal_encoding();
  2421. }
  2422. return mb_strstr($haystack, $needle, $part, $encoding);
  2423. }
  2424. }
  2425. // This is a multibyte replacement of stripos().
  2426. // This function exists in PHP 5 >= 5.2.0
  2427. // See http://php.net/manual/en/function.mb-stripos
  2428. if (MBSTRING_INSTALLED && !function_exists('mb_stripos')) {
  2429. function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) {
  2430. if (empty($encoding)) {
  2431. $encoding = mb_internal_encoding();
  2432. }
  2433. return mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), $offset, $encoding);
  2434. }
  2435. }
  2436. // This is a multibyte replacement of stristr().
  2437. // This function exists in PHP 5 >= 5.2.0
  2438. // See http://php.net/manual/en/function.mb-stristr
  2439. if (MBSTRING_INSTALLED && !function_exists('mb_stristr')) {
  2440. function mb_stristr($haystack, $needle, $part = false, $encoding = null) {
  2441. if (empty($encoding)) {
  2442. $encoding = mb_internal_encoding();
  2443. }
  2444. $pos = mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), 0, $encoding);
  2445. if ($pos === false) {
  2446. return false;
  2447. }
  2448. elseif($part == true) {
  2449. return mb_substr($haystack, 0, $pos + 1, $encoding);
  2450. } else {
  2451. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  2452. }
  2453. }
  2454. }
  2455. // This is a multibyte replacement of strrchr().
  2456. // This function exists in PHP 5 >= 5.2.0
  2457. // See http://php.net/manual/en/function.mb-strrchr
  2458. if (MBSTRING_INSTALLED && !function_exists('mb_strrchr')) {
  2459. function mb_strrchr($haystack, $needle, $part = false, $encoding = null) {
  2460. if (empty($encoding)) {
  2461. $encoding = mb_internal_encoding();
  2462. }
  2463. $needle = mb_substr($needle, 0, 1, $encoding);
  2464. $pos = mb_strrpos($haystack, $needle, mb_strlen($haystack, $encoding) - 1, $encoding);
  2465. if ($pos === false) {
  2466. return false;
  2467. } elseif($part == true) {
  2468. return mb_substr($haystack, 0, $pos + 1, $encoding);
  2469. } else {
  2470. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  2471. }
  2472. }
  2473. }
  2474. // This is a multibyte replacement of strstr().
  2475. // This function exists in PHP 5 >= 5.2.0
  2476. // See http://php.net/manual/en/function.mb-strstr
  2477. if (MBSTRING_INSTALLED && !function_exists('mb_strstr')) {
  2478. function mb_strstr($haystack, $needle, $part = false, $encoding = null) {
  2479. if (empty($encoding)) {
  2480. $encoding = mb_internal_encoding();
  2481. }
  2482. $pos = mb_strpos($haystack, $needle, 0, $encoding);
  2483. if ($pos === false) {
  2484. return false;
  2485. } elseif($part == true) {
  2486. return mb_substr($haystack, 0, $pos + 1, $encoding);
  2487. } else {
  2488. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  2489. }
  2490. }
  2491. }
  2492. ?>