• I have found a fairly accurate word/image counter, and have made it work nice on my blog. The only thing i don’t like is that the image counter is counting the smileys as well.. They’re a part of the text, and I don’t consider the nether text nor images… So what I would like to do is to modify this script so that it gives me the image count without counting the smileys… Guess it’s supposed to be something like NOT or something in the php, but I’m totally new to that… This is the code for the plugin:

    <?php
    /*
    Plugin Name: Word Count
    Plugin URI: https://www.jonas.rabbe.com/archives/2005/05/06/word-count-plugin-for-wordpress/
    Description: This plugin counts the number of words and images per post.
    Version: 1.0
    Author: Jonas Rabbe
    Author URI: https://www.jonas.rabbe.com/
    */

    // This just echoes the chosen line, we'll position it later
    function teb_word_count($display = true) {
    global $wpdb, $id;
    $post = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");

    $post_content = apply_filters('the_content', $post);
    $words = trim(strip_tags($post_content));
    if( $words == '' ) {
    $wordcount = 0;
    } else {
    $words = explode(' ', $words);
    $wordcount = count($words);
    }
    $ret_words = number_format($wordcount) . ' ' . ($wordcount == 1 ? 'ord' : 'ord');

    $imagecount = preg_match_all('/<img/', $post_content, $images);
    $ret_images = number_format($imagecount) . ' ' . ($imagecount == 1 ? 'bilde' : 'bilder');

    $text = $ret_words . ($imagecount > 0 ? ', ' . $ret_images : '');

    if( !$display ) {
    return $text;
    }

    echo $text;
    }
    ?>

    All response is appreciated ??

    My regards

Viewing 5 replies - 1 through 5 (of 5 total)
  • That plugin is based on my original plugin, but to make it not count smiley’s, you’d just have to tune the $imagecount line to be more restrictive.

    Currently it is looking just for the <img tag and counting them. The regular expression patter would have to be changed to look for <img but exclude the smiley directory (as an example).

    If you’re up to some coding/hacking. ?? You can check out:
    https://us4.php.net/preg_match_all

    And play around with the regex.

    Regards

    Thread Starter doffer

    (@doffer)

    The regex is just messy to me, but the only thing I need is to exclude is the smiley directory… If someone could just add a line to make it work, I would be very happy, since I’m no hacker/coder my self ??

    Here you go:
    $imagecount = preg_match_all('/<img/', $post_content, $images);
    $imagecount -= preg_match_all('/<img[^>]+(?=(class='wp-smiley'))/s', $post_content, $images);

    Thread Starter doffer

    (@doffer)

    Guess I misunderstood you, because something got wrong… I’m getting this error: Parse error: parse error, unexpected T_STRING in /usr/home/web/wno14743/blog/wp-content/plugins/teb-word-count.php on line 27

    <?php
    /*
    Plugin Name: Word Count
    Plugin URI: https://www.jonas.rabbe.com/archives/2005/05/06/word-count-plugin-for-wordpress/
    Description: This plugin counts the number of words and images per post.
    Version: 1.0
    Author: Jonas Rabbe
    Author URI: https://www.jonas.rabbe.com/
    */

    // This just echoes the chosen line, we'll position it later
    function teb_word_count($display = true) {
    global $wpdb, $id;
    $post = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");

    $post_content = apply_filters('the_content', $post);
    $words = trim(strip_tags($post_content));
    if( $words == '' ) {
    $wordcount = 0;
    } else {
    $words = explode(' ', $words);
    $wordcount = count($words);
    }
    $ret_words = number_format($wordcount) . ' ' . ($wordcount == 1 ? 'ord' : 'ord');

    $imagecount = preg_match_all('/<img/', $post_content, $images);
    $imagecount -=preg_match_all('/<img[^>]+(?=(class='wp-smiley'))/s', $post_content, $images);
    $ret_images = number_format($imagecount) . ' ' . ($imagecount == 1 ? 'bilde' : 'bilder');

    $text = $ret_words . ($imagecount > 0 ? ' og ' . $ret_images : '');

    if( !$display ) {
    return $text;
    }

    echo $text;
    }
    ?>

    My typo, sorry:

    $imagecount -= preg_match_all('/<img[^>]+(?=(class=\'wp-smiley\'))/s', $post_content, $images);

    edit: it wasn’t a typo after all, it was the forum software screwing up escaped code.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to exclude smileys from image count…’ is closed to new replies.