PHP to generate the tag cloud based on word frequency _php tips

Source: Internet
Author: User
Tags php programming

This article describes the PHP implementation of the word frequency to generate a tag cloud method. Share to everyone for your reference. Specifically as follows:

Given a piece of text, analyze the word frequency distribution of text, generate tag cloud

<?php/** * Tag Cloud demo based on word frequency * @author: Unknown * @since: 2007-02-27//Store frequency O
F words in an array $freqData = array ();
Random words $lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing. Phasellus vestibulum ullamcorper tortor. Aenean quis lacus quis neque adipiscing. Pellentesque tincidunt ligula vitae nibh ornare. 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 the AC fames turpis. 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 ac cras. 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 builds a frequency table foreach (Str_word_count ($lorem, 1) as $word) {//For each word fo und in the Frequency table,//increment it value by one array_key_exists ($word, $freqData)? $freqData [$word]++: $freqData
[$word]=0; }//==============================================================//= Function to actually generate the cloud from Prov ided data =//============================================================== FUnction Getcloud ($data = Array (), $minFontSize = $maxFontSize =) {$minimumCount = min (array_values ($data));
 $maximumCount = Max (Array_values ($data));
 $spread = $maximumCount-$minimumCount;
 $cloudHTML = ';
 $cloudTags = Array ();
 $spread = = 0 && $spread = 1; foreach ($data as $tag => $count) {$size = $minFontSize + ($count-$minimumCount) * ($maxFontSize-$minFontSi
 Ze)/$spread; $cloudTags [] = ' <a style= ' font-size: '. Floor ($size). ' px '. ' "class=" Tag_cloud "href=" http://www.google.com/search?q= ". $tag. "Title=". $tag. ' \ returned a count of '. $count. ' > '. Htmlspecialchars (Stripslashes ($tag)).
 ' </a> '; 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" >  

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.