1

I have this piece of code (running on Php 7) which will print out the frequency of words. The function works, however, special characters are not printed, probably because of str_word_count().

// Random words
$freqData = array();
$keywords = "Başka, Başka, küskün küskün otomobil kaçtı buraya küskün otomobil neden kaçtı
          kaçtı buraya, oraya KISMEN @here #there J.J.Johanson hep.
          Danny:Where is mom? I don't know! Café est weiß for 2 €uros.
          My 2nd nickname is mike18.";

// Set letter count to exclude words like 'and'/'or', etc.
$letterCount = 5;

// Get individual words and build a frequency table
foreach (str_word_count($keywords, 1) as $word) {
    // If the word has more than x letters
    if (mb_strlen($word, 'UTF-8') >= $letterCount) {
        // For each word found in the frequency table, increment its value by one
        array_key_exists($word, $freqData) ? $freqData[$word]++ : $freqData[$word] = 0;
    }
}
echo $freqData;

I have <meta charset="UTF-8"> in the <head> section. How can I solve this?

Meek
  • 3,086
  • 9
  • 38
  • 64
  • How do you "*print*" the special characters? Do you mean, that they are not counted for the `$letterCount` comparison, or do you litterally print them on screen and they are not shown? If it is the latter, please provide the code you use to *print* them. – Philipp Maurer Jan 26 '18 at 13:21
  • Using echo - I updated the code. – Meek Jan 26 '18 at 13:25
  • I ended up adding a charlist of letters like this: `foreach (str_word_count($keywords, 1, 'şçøæåé1233456789') as $word) {}` – Meek Jan 29 '18 at 08:18

0 Answers0