Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Getting A Count For Words In A Mysql Field, + Storing Php
This is a discussion on Getting A Count For Words In A Mysql Field, + Storing Php, within the MySQL section. This forum and the thread "Getting A Count For Words In A Mysql Field, + Storing Php" are both part of the Programming Your Website category.
![]() ![]() |
Nov 9 2008, 08:57 AM
Post
#1
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
I was wanting to do some word analysis on content we have created. Basically, get some stats on the words are used in a content piece.
For example, considering the following body: <p>"Hello I am a hello".</p> Eventual analysis would look something like: Hello(2) I(1) am(1) a(a) In the example I put in html because the content that is being analysed is in fact html stored in a mysql table. I would not be interested in knowing the html so I would filter that out. I assume this would be done in php but I have seen some things done in mysql done so quickly I didn't know it was possible so am open to a solution either way. Also, is there a special way one goes about storing php inside of a table, or a special process for echoing out php script from a table that makes it function as php and not print the script on screen? lol |
|
|
Nov 15 2008, 06:51 AM
Post
#2
|
|
|
Squeezing ![]() ![]() ![]() Posts: 81 Joined: 18-February 08 |
I was wanting to do some word analysis on content we have created. Basically, get some stats on the words are used in a content piece. For example, considering the following body: <p>"Hello I am a hello".</p> Eventual analysis would look something like: Hello(2) I(1) am(1) a(a) In the example I put in html because the content that is being analysed is in fact html stored in a mysql table. I would not be interested in knowing the html so I would filter that out. I assume this would be done in php but I have seen some things done in mysql done so quickly I didn't know it was possible so am open to a solution either way. Also, is there a special way one goes about storing php inside of a table, or a special process for echoing out php script from a table that makes it function as php and not print the script on screen? lol I don't know much sql or regex, so there's probably a better way of doing this, but this should do the job in php: CODE <pre> <?php $string = '<p>"Hello I am a hello".</p> <div style="border: 1px solid #000">Hello don\'t you think think</div>'; $string = preg_replace("/<([^>]+)>/i", '', $string); //Delete all tags in the string $results = array(); //Initialize an array to hold our results while(preg_match('/(\b[a-z\']+\b)/i', $string, $matches)) //While we can match any words with a-z or a ' in them do the following ($matches holds the item we have matched) { $string = preg_replace("/\b{$matches[0]}\b/i", '', $string, -1, $count); //Remove any instances of the word we just matched, so we don;t keep continually matching the same word. $count holds how many replacements of the word occur. $results[$matches[0]] = $count; //Update the results array so the key is the word matched and the value is the amount of times that word occured } print_r($results); //Print the results ?> </pre> As far as I'm aware you can't execute php code stored in a database, but again I don't know much about this so could be wrong. Dave |
|
|
Nov 15 2008, 09:53 AM
Post
#3
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
As far as I know, there is no way to perform this sort of analysis within the database. You can perform full index searches or build an index out the content in the database but not count each occurrence.
Dave's example is good and should work. I could possibly tweak it a bit though to remove reliance on preg_* functions which are a bit slower -- especially if you're going to analyse a large chunk. CODE $string = strip_tags($string); // Remove all HTML tags from the text. Assuming the markup is valid, this will get everything // Now, to avoid getting words bundled in with punctuation, remove all punctuation -- with the exception of hyphens or your list of characters $string = preg_replace('/[^\w\-]+/im', $string); // Get an array of all words $string = preg_split('/[\s]+/', $string); // Use preg split in case there are multiple spaces between some words // Start counting $counts = array() foreach($string as $key => $word): if (empty($string[$word])) $counts[$word] = 1; else $counts[$word]++; endforeach; unset($string); echo '<pre>'; print_r($counts); echo '</pre>'; As for storing PHP in a database, you only need to store it as text. When you retrieve it you can run it using the eval function: CODE eval($string); Obviously, you don't want to go throwing this function around too often. If you find you have to eval something (apart from code in a database), you're probably going about it in the wrong way. -------------------- |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
9 | craig | 718 | 14th February 2008 - 11:54 AM Last post by: Simon |
|||
![]() |
4 | Jason | 268 | 15th February 2008 - 05:54 PM Last post by: Linda |
|||
![]() |
3 | thewal | 402 | 19th February 2008 - 03:32 PM Last post by: JasonStanley |
|||
![]() |
22 | thesealportalteam | 664 | 20th March 2008 - 09:12 AM Last post by: Monie |
|||
![]() |
3 | 1christopher | 413 | 23rd February 2008 - 10:18 AM Last post by: 1christopher |
|||






Nov 9 2008, 08:57 AM










