Feb 1st, 2006, 07:30 AM
#1
Thread Starter
Addicted Member
word occurence
I know fopen function is to open website (handle) but don't know how to search for number of occurences of particular word.
Would you help me ?
Thank You.
Feb 1st, 2006, 07:43 AM
#2
<?="Moderator"?>
Re: word occurence
Look at preg_match and preg_match_all, it allows you to match all instances of words and patterns
http://uk.php.net/manual/en/function.preg-match.php
Feb 1st, 2006, 03:41 PM
#3
Re: word occurence
A few problems with preg... it will match test in testing for example easy too fix but try something like this:
PHP Code:
<?php
function NumInstancesOfWord ( $HayStack , $Needle , $MatchCase = false )
{
$WordArray = array();
# Make shure nasty chars don't affect the results.
$HayStack = preg_replace ( '#[^a-z0-9 ]#si' , '' , $HayStack );
if ( $MatchCase == false ) # Case insensative
{
$WordArray = explode ( ' ' , strtolower ( $HayStack ));
$Needle = strtolower ( $Needle );
}
else
{
$WordArray = explode ( ' ' , $HayStack );
}
$CountVar = 0 ;
$i = 0 ;
while ( $WordArray [ $i ])
{
if ( $WordArray [ $i ] == $Needle )
{
++ $CountVar ;
}
++ $i ;
}
return ( $CountVar );
}
?>
Example:
PHP Code:
<?php
echo NumInstancesOfWord ( 'This is a test, testing 123, testing 1123' , 'test' ); # Returns 1
echo NumInstancesOfWord ( 'This is a test, testing 123, testing 1123' , 'testing ' ); # Returns 2
?>
Cheers,
Ryan Jones
Last edited by sciguyryan; Feb 1st, 2006 at 03:45 PM .
Feb 1st, 2006, 03:48 PM
#4
Thread Starter
Addicted Member
Feb 1st, 2006, 03:50 PM
#5
Thread Starter
Addicted Member
Re: word occurence
and both are returning 1.
Feb 1st, 2006, 04:03 PM
#6
Re: word occurence
Originally Posted by
slice
and both are returning 1.
http://standards.spiralmindsinc.com/Testing/Test.php
There are 2 instances of testing.
There are 1 instances of test.
This is a test, testing 123, testing 1123
Exact code in file attatched.
Cheers,
Ryan Jones
Attached Files
Feb 1st, 2006, 04:12 PM
#7
Thread Starter
Addicted Member
Re: word occurence
You must spread some Reputation around before giving it to sciguyryan again.
THANKS A LOT
Feb 1st, 2006, 04:15 PM
#8
Re: word occurence
Originally Posted by
slice
You must spread some Reputation around before giving it to sciguyryan again.
THANKS A LOT
No problem
Cheers,
Ryan Jones
Feb 1st, 2006, 04:47 PM
#9
Thread Starter
Addicted Member
Re: word occurence
EEEEEEK
Another problem/question now i want to use this function for counting words on particular website page like www.anyname.com/home.asp ...
I am dump again
It is always returning me 0 result.
Feb 1st, 2006, 04:51 PM
#10
Re: word occurence
Originally Posted by
slice
EEEEEEK
Another problem/question now i want to use this function for counting words on particular website page like
www.anyname.com/home.asp ...
I am dump again
It is always returning me 0 result.
I'm not shure what you mean, the name of the site or the contents of the file?
If the second the contents can be grabbed as follows:
PHP Code:
<?php
$Contents = implode ( '' , file ( 'www.anyname.com/home.asp' ));
?>
Then just plug that in as a parameter
Cheers,
Ryan Jones
Feb 1st, 2006, 05:04 PM
#11
Thread Starter
Addicted Member
Re: word occurence
Not working
Code:
<?php
function NumInstancesOfWord($HayStack, $Needle, $MatchCase = false)
{
$WordArray = array();
# Make shure masty chars don't affect the results.
$HayStack = preg_replace('#[^a-z0-9 ]#si', '', $HayStack);
if ($MatchCase == false) # Case insensative
{
$WordArray = explode(' ', strtolower($HayStack));
$$Needle = strtolower($Needle);
}
else
{
$WordArray = explode(' ', $HayStack);
}
$CountVar = 0;
$i = 0;
while ($WordArray[$i])
{
if ($WordArray[$i] == $Needle)
{
++$CountVar;
}
++$i;
}
return ($CountVar);
}
$TestStr = 'This is a test, test ing 123, testing 1123';
echo ('There are ' . NumInstancesOfWord($TestStr, 'testing') . ' instances of testing.<br />');
echo ('There are ' . NumInstancesOfWord($TestStr, 'test') . ' instances of test.<br />');
$Contents = implode('', file('http://vbforums.com/showthread.php?p=2338266#post2338266'));
//echo($Contents);
echo ('There are ' . NumInstancesOfWord($Contents, 'slice') . ' instances of slice.<br />');
// echo ($TestStr);
?>
Result
There are 0 instances of slice
Feb 1st, 2006, 05:06 PM
#12
Thread Starter
Addicted Member
Re: word occurence
Not working
PHP Code:
<?php
function NumInstancesOfWord ( $HayStack , $Needle , $MatchCase = false )
{
$WordArray = array();
# Make shure masty chars don't affect the results.
$HayStack = preg_replace ( '#[^a-z0-9 ]#si' , '' , $HayStack );
if ( $MatchCase == false ) # Case insensative
{
$WordArray = explode ( ' ' , strtolower ( $HayStack ));
$ $Needle = strtolower ( $Needle );
}
else
{
$WordArray = explode ( ' ' , $HayStack );
}
$CountVar = 0 ;
$i = 0 ;
while ( $WordArray [ $i ])
{
if ( $WordArray [ $i ] == $Needle )
{
++ $CountVar ;
}
++ $i ;
}
return ( $CountVar );
}
$TestStr = 'This is a test, test ing 123, testing 1123' ;
echo ( 'There are ' . NumInstancesOfWord ( $TestStr , 'testing' ) . ' instances of testing.<br />' );
echo ( 'There are ' . NumInstancesOfWord ( $TestStr , 'test' ) . ' instances of test.<br />' );
$Contents = implode ( '' , file ( 'http://vbforums.com/showthread.php?p=2338266#post2338266' ));
//echo($Contents);
echo ( 'There are ' . NumInstancesOfWord ( $Contents , 'slice' ) . ' instances of slice.<br />' );
// echo ($TestStr);
?>
Result
There are 0 instances of slice
Feb 1st, 2006, 05:29 PM
#13
Re: word occurence
This version seems too work better.
The problem was that I checked for only the character, not the instance of the needle.
Cheers,
Ryan Jones
Attached Files
Feb 1st, 2006, 05:32 PM
#14
Thread Starter
Addicted Member
Re: word occurence
You are my hero
Thanks a lot.I will learn from you in future too.
Thank You once again.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width