DaveChild\TextStatistics\Text::letterCount PHP Method

letterCount() public static method

Gives letter count (ignores all non-letters). Tries mb_strlen and if that fails uses regular strlen.
public static letterCount ( string $strText, string $strEncoding = '' ) : integer
$strText string Text to be measured
$strEncoding string Encoding of text
return integer
    public static function letterCount($strText, $strEncoding = '')
    {
        if (strlen(trim($strText)) == 0) {
            return 0;
        }
        if (is_null(self::$blnMbstring)) {
            self::$blnMbstring = extension_loaded('mbstring');
        }
        $strText = self::cleanText($strText);
        // To clear out newlines etc
        $intTextLength = 0;
        $strText = preg_replace('`[^A-Za-z]+`', '', $strText);
        try {
            if (!self::$blnMbstring) {
                throw new Exception('The extension mbstring is not loaded.');
            }
            if ($strEncoding == '') {
                $intTextLength = mb_strlen($strText);
            } else {
                $intTextLength = mb_strlen($strText, $strEncoding);
            }
        } catch (\Exception $e) {
            $intTextLength = strlen($strText);
        }
        return $intTextLength;
    }

Usage Example

 /**
  * Returns the text statistics for an article.
  *
  * @param string $articleId
  * @param string $template
  *
  * @return TextStatisticsModel
  */
 public function getTextStatistics($articleId = '', $wpm = null, $readingTimeTemplate = null)
 {
     if (!isset($this->articlesData[$articleId])) {
         return null;
     }
     $articleData =& $this->getArticleDataById($articleId);
     $wordCount = Text::wordCount($articleData['text']);
     $wpm = $wpm ?: craft()->config->get('wordsPerMinute', 'textStatistics');
     // Calculate times in seconds
     $textReadingTime = $wordCount / $wpm * 60;
     $additionalTime = $articleData['additionalTime'];
     $totalTime = ceil($textReadingTime + $additionalTime);
     $readingTime = $this->getDateIntervalFromSeconds($totalTime);
     $readingTimeMinutes = ceil($totalTime / 60);
     // Render Twig template for the readingTimeString
     $variables = ['iv' => $readingTime, 'min' => $readingTimeMinutes];
     $template = $readingTimeTemplate ?: craft()->config->get('readingTimeTemplate', 'textStatistics');
     $readingTimeString = craft()->templates->renderString($template, $variables);
     // Populate Statistics model
     $model = new TextStatisticsModel();
     $model->textLength = Text::textLength($articleData['text']);
     $model->letterCount = Text::letterCount($articleData['text']);
     $model->syllableCount = Syllables::syllableCount($articleData['text']);
     $model->wordCount = $wordCount;
     $model->sentenceCount = Text::sentenceCount($articleData['text']);
     $model->readingTime = $readingTime;
     $model->readingTimeMinutes = $readingTimeMinutes;
     $model->readingTimeString = $readingTimeString;
     $textStatistics = $this->getTextStatisticsClass();
     $model->gunningFog = $textStatistics->gunningFogScore($articleData['text']);
     $model->fleschKincaid = $textStatistics->fleschKincaidReadingEase($articleData['text']);
     if (!$model->validate()) {
         throw new Exception('There was an error while generating the Text Statistics.');
     }
     return $model;
 }