search
Homephp教程PHP源码PHP generates tag cloud based on word frequency

Given a piece of text, this code analyzes the word frequency distribution of the text and generates a tag cloud

<?php
   
// Store frequency of words in an array
$freqData = array();
   
// Random words
$lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Phasellus vestibulum ullamcorper tortor. Aenean quis lacus quis neque
adipiscing ultricies. Pellentesque tincidunt ligula vitae nibh ornare
pharetra. Proin dignissim tortor. Donec et ipsum nec tellus gravida
tempor. Aliquam ullamcorper purus vel felis. Praesent faucibus.
Curabitur porta. Nulla in lorem quis mi lacinia fringilla. Integer
adipiscing mi quis felis. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Quisque sagittis ante in
arcu. Sed libero enim, venenatis sit amet, vestibulum at, porttitor id,
neque. Vestibulum ornare semper erat. Sed tincidunt nibh et massa. Cras
sed diam. Quisque blandit enim.
   
   
Sed nonummy. Aenean mollis turpis quis enim. Nam massa nulla, varius
molestie, aliquet et, feugiat eget, nisi. Sed mollis, leo ut pretium
placerat, nibh turpis egestas ipsum, sed aliquam neque enim in risus.
Nullam nisl. Sed tincidunt leo quis tellus. Mauris non lorem. Aenean
tristique justo at arcu. Fusce et lorem. Nam sodales. Mauris condimentum
diam. Nam commodo. Cum sociis natoque penatibus et magnis dis parturient
montes, nascetur ridiculus mus. Cras ac risus. Proin et dolor laoreet mi
gravida sodales. Duis bibendum, ipsum posuere egestas posuere, dui lacus
feugiat turpis, id tincidunt urna est sit amet est. Cras eu sem.
";
   
// Get individual words and build a frequency table
foreach( str_word_count( $lorem, 1 ) as $word )
{
    // For each word found in the frequency table, increment its value by one
    array_key_exists( $word, $freqData ) ? $freqData[ $word ]++ : $freqData[ $word ] = 0;
}
   
// ==============================================================
// = Function to actually generate the cloud from provided data =
// ==============================================================
function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
{
    $minimumCount = min( array_values( $data ) );
    $maximumCount = max( array_values( $data ) );
    $spread       = $maximumCount - $minimumCount;
    $cloudHTML    = &#39;&#39;;
    $cloudTags    = array();
   
    $spread == 0 && $spread = 1;
   
    foreach( $data as $tag => $count )
    {
        $size = $minFontSize + ( $count - $minimumCount )
            * ( $maxFontSize - $minFontSize ) / $spread;
        $cloudTags[] = &#39;<a style="font-size: &#39; . floor( $size ) . &#39;px&#39;
        . &#39;" class="tag_cloud" href="http://www.google.com/search?q=&#39; . $tag
        . &#39;" title="\&#39;&#39; . $tag  . &#39;\&#39; returned a count of &#39; . $count . &#39;">&#39;
        . htmlspecialchars( stripslashes( $tag ) ) . &#39;</a>&#39;;
    }
   
    return join( "\n", $cloudTags ) . "\n";
}  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Tag Cloud Demo</title>
    <style type="text/css" media="screen">
        /*<![CDATA[*/
        .tag_cloud { padding: 3px; text-decoration: none; }
        .tag_cloud:link  { color: #81d601; }
        .tag_cloud:visited { color: #019c05; }
        .tag_cloud:hover { color: #ffffff; background: #69da03; }
        .tag_cloud:active { color: #ffffff; background: #ACFC65; }
        /*]]>*/
    </style>
</head>
   
<body>
    <h1 id="Sample-nbsp-Tag-nbsp-Cloud">Sample Tag Cloud</h1>
    <div id="wrapper">
        <?php echo getCloud( $freqData ) ?>
    </div>
</body>
</html>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)