0

I created an A-Z index for my wordpress dictionary website. I've got an issue when accents are used on some letters.

I used sanitize to avoid this but now every word beggining with an E is like another letter instead of being grouped. How can I group every word begining with an E regardless of is there an accent or not ?

Here's the link to my page : http://preprod2.atelier-labotte.fr/dictionnaire/

Here's the code I used for creating the index :

<?php

    $letter = '';
    while($dictionnaire->have_posts())
    {
        $dictionnaire->the_post();

        // Check the current letter is the same that the first of the title
        if($letter != strtoupper(get_the_title()[0]))
        {

            echo ($letter != '') ? '</div></section>' : '';
            $letter = strtoupper(substr(sanitize_title( get_the_title() ),0,1));
            
            echo '<section id="'. $letter .'" class="wp-block-group letter-section"><div class="big-letter"><p>'.strtoupper(substr(sanitize_title( get_the_title() ),0,1)).'</p></div><div class="wp-block-group__inner-container d-grid columns-3">';   
         
        } 
?>

Thanks for your help !

PS : I'm non-english native speaker, please forgive my mistkakes !

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Maybe you should make the same change in _both_ places? You did not apply the same sanitization here: `if($letter != strtoupper(get_the_title()[0]))` – CBroe Apr 05 '22 at 11:56
  • You should use the function _mb_strtoupper_ and set the second parameter to 'UTF-8' and not strtoupper https://www.php.net/manual/fr/function.mb-strtoupper.php#example-2592 – NovaŜancO Apr 06 '22 at 11:46
  • Hi @CBroe, Thanks for you help ! I already tried what you suggested but when I apply sanitize action on the if condition, it breaks the loop. That's why I didn't use the same change in the both places. – Raphael Vallauri Apr 07 '22 at 08:43
  • Breaks the loop how, exactly? _"That's why I didn't use the same change in the both places."_ - but that makes no sense. You are trying to compare the first letter of the current item title, with that of the previous one. If you don't apply the same sanitization in _both_ places, how is that supposed to work? Assume the first item title started with `E`, that is still an `E` after sanitization, so `$letter` contains `E` now. Now we get to your second item, the title of that one starts with `É`. So you are comparing `E` with `É` at this point*, which of course is false. – CBroe Apr 07 '22 at 08:49
  • The * after "at this point", because `get_the_title()[0]` is wrong to begin with - this is _byte_-based access, not character-based access. And `É` is a multibyte character in UTF-8. Go check [Get first character of UTF-8 string](https://stackoverflow.com/q/13508937/1427878) on how to handle this properly. – CBroe Apr 07 '22 at 08:51
  • @CBroe Thanks for you explanations, as I'm designer and PHP is not my main skill, I didn't understand exactly what's the effect of all lines but it's clearer to me. When I said that loop breaks, it's just that the if statement didn't return anything (no loop after the A -Z index). I make some changes and I'm now near to the goal: all "É" value are in the same group. I just need to make "É" and "E" the same letter. Here's the code I now use in all places : `mb_strtoupper(mb_substr(get_the_title(), 0,1 , 'UTF-8'))` – Raphael Vallauri Apr 07 '22 at 09:27

1 Answers1

0

Thanks for your help !

I find a way to reach what I needed so I share the solution. I hope this is the simplest way to achieve this but here's what I used :

<?php
$letter = '';
while($dictionnaire->have_posts())
{
    $dictionnaire->the_post();

    // Check the current letter is the same that the first of the title
    if($letter != strtoupper(sanitize_title(mb_strtoupper(mb_substr(get_the_title(), 0,1 , 'UTF-8')))))
    {

        echo ($letter != '') ? '</div></section>' : '';
        $letter = strtoupper(sanitize_title(mb_strtoupper(mb_substr(get_the_title(), 0,1, 'UTF-8'))));
        
        echo '<section id="'. $letter .'" class="wp-block-group letter-section"><div class="big-letter"><p>'.$letter.'</p></div><div class="wp-block-group__inner-container d-grid columns-3">';            
    } 
?>