SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Count number of occurrences of word in meta tag.

    Hi guys,

    I am experimenting with self made SEO tools at the mo.

    I want to retrieve meta tags from several websites and count the number of occurrences of each word (probably to create a tag cloud).

    I can easily get the title and meta tags (regex and get_meta_tags respectively) but how would I go about counting them?

    In my head I envisage retrieving all the data concatenating (.=) it all into one string. Then processing, and eventually getting an array like this:
    PHP Code:
    Array
    (
        [
    word1] => 2
        
    [word2] => 1
        
    [word3] => 5
        
    [word4] => 1
     

    Where the key is the word counted and the value is the number of occurrences.

    Any ideas?

    Many thanks,


    atw

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sounds about right, except the string shenanigans. Shove all the meta keywords into an array and pass to array_count_values like so.
    PHP Code:
    <?php
    $aMeta 
    = array(
        
    'dog',
        
    'cat',
        
    'mouse',
        
    'cheese',
        
    'dog',
        
    'dog',
        
    'cat'
    );
    print_r(array_count_values($aMeta));
    /*
        Array
        (
            [dog] => 3
            [cat] => 2
            [mouse] => 1
            [cheese] => 1
        )
    */
    ?>
    Au Voila!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mk_awesome( 'SilverBulletUK' );



    Superb, it works like a dream. I didn't even know that that function existed (you learn something new every day)!

    Many thanks...


  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Me neither heh, found it on the manual today. I guess we both learnt something today!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •