Site logo

Archived topic

reading time php (little help if possible)

21 replies · Started by johnaps on January 9, 2022

Viewing posts 16–22 of 22

Try this function which is going to count the spaces between words instead:

function tu_estimated_reading_time() {
    ob_start();
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = count(preg_split('/s+/', $clean_content ));
    $time ='<div class="read-time"><span class="gp-icon">•</span>Reading time: '. ceil( $word_count / $wpm ) . ' minutes'. '</div>';

    var_dump($word_count);

    return  $time; 
    return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');

Note i left the var_dump($word_count); to output the word count.

Strange - what happens if you try it on a test post written in English?

Having a hard time finding a solution for this, having dug around stack overflow i found this for counting non-word characters, which may work in this instance. Try using it like so:

function tu_estimated_reading_time() {
    ob_start();
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = count(preg_split('/\P{L}/usi', $clean_content, -1, , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
    $time ='<div class="read-time"><span class="gp-icon">•</span>Reading time: '. ceil( $word_count / $wpm ) . ' minutes'. '</div>';

    var_dump($word_count);

    return  $time; 
    return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');

You CAME through!

Thank you very much for all the efforts, you made this possible for me, so i am much obliged...

I wouldnt have been able to come up with this myself...

A PERFECT SOLUTION FOR COUNTING WORDS FOR GREEK LANGUAGE ON POSTS.

Thank you very much! :)

Please share with me, where i can make a review for gp team support.

This archived topic is closed to new replies.