2

I got some search results listed alphabetically by groups like this:

A -- Andre, Amalia...

B -- Bart, Bruno, etc...

The problem is, if some name starts with a special character like "Ólga" i got another group like this:

Ó -- Olga

My question: How to list names starting with "Ó" inside the group "O"? Thanks.

My code:

<?php
$previous = null;
foreach($rows as $row1) {
 $firstLetter = substr($row1->titolo, 0, 1);
  if($previous !== $firstLetter) echo $firstLetter."<br />";
$previous = $firstLetter;
  echo $row1->titolo;
}
?>

2 Answers2

3

In PHP you can use the Collator class

Provides string comparison capability with support for appropriate locale-sensitive sort orderings

http://docs.php.net/manual/en/class.collator.php

Bert
  • 2,134
  • 19
  • 19
  • yeap...by far better that previous answers like https://stackoverflow.com/questions/6837148/change-foreign-characters-to-their-normal-equivalent – oetoni Nov 14 '17 at 16:57
  • 2
    Looks like a useful tool, but how would you use the Collator class to solve this specific problem? – Don't Panic Nov 14 '17 at 17:00
0

The class Collator is part of the intl extension, so if you don't have it or you can't install it, I suggest you to use this utility conversion array in this answer Replacing accented characters php

<?php
$unwanted_array = array('Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
                        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
                        'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
                        'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
                        'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
$previous = null;
foreach($rows as $row1) {
    $firstLetter = substr($row1->titolo, 0, 1);
    $firstLetter = strtr( $firstLetter, $unwanted_array );
    if($previous !== $firstLetter) echo $firstLetter."<br />";
    $previous = $firstLetter;
    echo $row1->titolo;
}
?>
Danilo Carrabino
  • 397
  • 6
  • 12