src/Bookworm/Bookworm.php
private static function countCode($text)
    {
        // remove code attribute content, like from href="" or d=""
        $text = preg_replace('/"[^"]*"/i', '', $text);
        // get markdown code
        $regex = '/(?<=(?<!`))`([^`\n\r]+)`(?=(?!`))|```[a-zA-Z]*([^`]*)```/i';
        $markdownCount = preg_match_all($regex, $text, $markdownMatches, PREG_PATTERN_ORDER);
        // Remove markdown code from text, as to not double count
        $text = trim(preg_replace('/(?<=(?<!`))`[^`\n\r]+`(?=(?!`))|```[a-zA-Z]*[^`]*```/i', ' ', $text));

        // get html code elements
        $regex = '/<code>([\s\S]*?)<\/code>/i';
        $htmlCount = preg_match_all($regex, $text, $htmlMatches, PREG_PATTERN_ORDER);

        // check if any matches exist
        if ($markdownCount === 0 && $htmlCount === 0) {
            return 0;
        }
        // concat all code
        $code = implode($markdownMatches[1], ' ').' '.implode($markdownMatches[2], ' ').' '.implode($htmlMatches[1], ' ');

        // replace multiple spaces
        $code = preg_replace(['/\s+/', '/^\s/'], [' ', ''], $code);

        // return the number words in the code blocks
        return count(array_filter(explode(' ', $code)));
    }
private static function countCode($text)
    {
        // remove code attribute content, like from href="" or d=""
        $text = preg_replace('/"[^"]*"/i', '', $text);
        // get markdown code
        $regex = '/(?<=(?<!`))`([^`\n\r]+)`(?=(?!`))|```[a-zA-Z]*([^`]*)```/i';
        $markdownCount = preg_match_all($regex, $text, $markdownMatches, PREG_PATTERN_ORDER);
        // Remove markdown code from text, as to not double count
        $text = trim(preg_replace('/(?<=(?<!`))`[^`\n\r]+`(?=(?!`))|```[a-zA-Z]*[^`]*```/i', ' ', $text));

        // get html code elements
        $regex = '/<code>([\s\S]*?)<\/code>/i';
        $htmlCount = preg_match_all($regex, $text, $htmlMatches, PREG_PATTERN_ORDER);

        // check if any matches exist
        if ($markdownCount === 0 && $htmlCount === 0) {
            return 0;
        }
        // concat all code
        $code = implode($markdownMatches[1], ' ').' '.implode($markdownMatches[2], ' ').' '.implode($htmlMatches[1], ' ');

        // replace multiple spaces
        $code = preg_replace(['/\s+/', '/^\s/'], [' ', ''], $code);

        // return the number words in the code blocks
        return count(array_filter(explode(' ', $code)));
    }
private static function countCode ( $ text ) { // remove code attribute content, like from href="" or d="" $ text = preg_replace ( '/"[^"]*"/i' , '' , $ text ) ; // get markdown code $ regex = '/(?<=(?<!`))`([^`\n\r]+)`(?=(?!`))|```[a-zA-Z]*([^`]*)```/i' ; $ markdownCount = preg_match_all ( $ regex , $ text , $ markdownMatches , PREG_PATTERN_ORDER ) ; // Remove markdown code from text, as to not double count $ text = trim ( preg_replace ( '/(?<=(?<!`))`[^`\n\r]+`(?=(?!`))|```[a-zA-Z]*[^`]*```/i' , ' ' , $ text ) ) ; // get html code elements $ regex = '/<code>([\s\S]*?)<\/code>/i' ; $ htmlCount = preg_match_all ( $ regex , $ text , $ htmlMatches , PREG_PATTERN_ORDER ) ; // check if any matches exist if ( $ markdownCount === 0 && $ htmlCount === 0 ) { return 0 ; } // concat all code $ code = implode ( $ markdownMatches [ 1 ] , ' ' ) . ' ' . implode ( $ markdownMatches [ 2 ] , ' ' ) . ' ' . implode ( $ htmlMatches [ 1 ] , ' ' ) ; // replace multiple spaces $ code = preg_replace ( [ '/\s+/' , '/^\s/' ] , [ ' ' , '' ] , $ code ) ; // return the number words in the code blocks return count ( array_filter ( explode ( ' ' , $ code ) ) ) ; }
Counts how many "code words" are in a specific text.

@param string $text The text from which the words should be counted

@return int
Counts how many code words are in a specific text .