Search
 
SCRIPT & CODE EXAMPLE
 

PHP

PHP strcoll — Locale based string comparison

You should not rely on this function to properly compare localized strings.

<?php
$a = "Österreich";
$b = "Oesterreich";
$z = "Zeta";

echo setlocale(LC_ALL, 0) . PHP_EOL; // (on my mac: C/en_US.UTF-8/C/C/C/C)
echo strcoll($a, $b) . PHP_EOL; // 116
echo strcoll($b, $a) . PHP_EOL; // -116
echo strcoll($a, $z) . PHP_EOL; // 105

echo setlocale(LC_ALL, "de_DE") . PHP_EOL; // de_DE
echo strcoll($a, $b) . PHP_EOL; // 135
echo strcoll($b, $a) . PHP_EOL; // -135
echo strcoll($a, $z) . PHP_EOL; // 124

$collator = new Collator("de_DE");
echo $collator->compare($a, $b); // 1
echo $collator->compare($b, $a); // -1
echo $collator->compare($a, $z); // -1
?>

Using the Collator (from the intl module) you will get the expected result for e.g. sorting such that the string "Österreich" will rank higher than "Zeta", but after "Oesterreich".

strcoll's output will differ per platform, locale and used c library, while the Collator will give more stable results on different platforms.
Comment

PREVIOUS NEXT
Code Example
Php :: php html text before first h2 tag 
Php :: laravel return redirect back with input except one filed 
Php :: laravel Call to a member function validate() on array 
Php :: laravel htaccess to remove public from url 
Php :: Custom search form 
Php :: php current url 
Php :: add class to li 
Php :: Check Data Load More Laravel Livewire 
Php :: IgasterLaravel ThemeExceptions heme Already Exists 
Php :: creating custom database 
Php :: laravel count the types of users 
Php :: utf8mb4 decode in php 
Php :: Add a watermark to a new PDF document 
Php :: how to pass value in app.blade 
Php :: modifier laravel migration to add a comment a column (MySQL/PostgreSQL) 
Php :: redirect www to non-www wordpress multisite 
Php :: nginx phpmyadmin subdirectory 
Php :: how to set tinyint default 0 laravel migration 
Php :: array with key value pair php 
Php :: .htaccess Preventing access to your PHP includes files 
Php :: Differentiate PHP include and require statement 
Php :: how to get textbox value in php without submit 
Php :: how to lookup value object php 
Php :: laravel join with count 
Php :: php loop array PDO remove keys 
Php :: onde que fica a praia escondida no roblox jo mulher maravilha 
Php :: how to use db more than 1 codeigiter 3 
Php :: wp_handle_upload return uploaded file name 
Php :: symfony server:start not working 
Php :: php echo comand 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =