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.
Printable View
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.
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
A few problems with preg... it will match test in testing for example easy too fix but try something like this:
Example: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);
}
?>
Cheers,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
?>
Ryan Jones
Undefined offset: 8
and both are returning 1.
http://standards.spiralmindsinc.com/Testing/Test.phpQuote:
Originally Posted by slice
Exact code in file attatched.Quote:
There are 2 instances of testing.
There are 1 instances of test.
This is a test, testing 123, testing 1123
Cheers,
Ryan Jones
You must spread some Reputation around before giving it to sciguyryan again.
THANKS A LOT
No problem :)Quote:
Originally Posted by slice
Cheers,
Ryan Jones
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 :confused:
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?Quote:
Originally Posted by slice
If the second the contents can be grabbed as follows:
Then just plug that in as a parameter :)PHP Code:<?php
$Contents = implode('', file('www.anyname.com/home.asp'));
?>
Cheers,
Ryan Jones
Not working :rolleyes:
ResultCode:<?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);
?>
Quote:
There are 0 instances of slice
Not working :rolleyes:
ResultPHP 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);
?>
Quote:
There are 0 instances of slice
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
You are my hero
Thanks a lot.I will learn from you in future too.
Thank You once again.