Hi Ryan,
I think the newest versions of PHP have a function called "substr_count", but the following code is a bit more flexible, and also works with older versions of PHP. You can copy/paste it into a *.php file and try it from your webserver.
Hope this helps,
Shanx
(shanx@shanx.com)
-----------------CODE-----------------
<?php
/**
Function to count the number of times a string appears in another
one. Also can specify if an exact match is desired.
@ $full The fill string in which to search
@ $delim The string delimiter (usually a space!)
@ $search The word to search for
@ $exact Should we look for exact words?
@return The count of the occurences (could be 0) or
a negative in case of error
@author Shanx [shanx@shanx.com]
@version 0.1
/
function substrCount($full, $delim, $search, $exact = "false", $caseSensitive = "false")
{
// If case-sensitive is needed, then do nothing,
// otherwise make both the string as well as the
// search word lower case, so there is no diff
$full = ($caseSensitive == "false") ? strtolower($full) : $full;
$search = ($caseSensitive == "false") ? strtolower($search) : $search;
// First tokenize the string
$returnCount = 0; // Initialize the return count
$tok = strtok ($full, $delim); // First time tokenization!
// If an exact match is desired, then loop through
// and compare the two strings EXACTLY.
if ($exact == "true")
{
//---------------- EXACT MATCH -----------------
// In this case, ONLY search for the exact word
//----------------------------------------------
while ($tok)
{
if ($tok == $search) $returnCount++;
$tok = strtok ($delim);
}
}
else
{
//------------ NON EXACT MATCH -----------------
// In this case, even if the search word is BOY
// and a part of the overall sentence is BOYS,
// it will be returned
//----------------------------------------------
while ($tok)
{
// I use strspn and not strpos because
// at least that is the same in all PHP
// versions!
$pos = strspn($tok, $search);
// A helpful note about STRSPN:
// strspn("boy", "boys") will return 3
// strspn("boy", "this") will return 0 - because the string "boy" is not found in "this"
// strspn("boy", "but") will return 1! - because the char "b" of boy is found in "but"
if ($pos == strlen($search))
{
$returnCount++;
}
$tok = strtok ($delim);
}
}
return $returnCount;
}
$fullString = (isset($HTTP_POST_VARS["fullString"])) ? $HTTP_POST_VARS["fullString"] : "Shanx is a good boy but boys will be boys.";
$fullStringDelim = (isset($HTTP_POST_VARS["fullStringDelim"])) ? $HTTP_POST_VARS["fullStringDelim"] : " " ;
$stringToSearch = (isset($HTTP_POST_VARS["stringToSearch"])) ? $HTTP_POST_VARS["stringToSearch"] : "boy";
$exactMatch = (isset($HTTP_POST_VARS["exactMatch"])) ? $HTTP_POST_VARS["exactMatch"] : "false";
$caseSensitive = (isset($HTTP_POST_VARS["caseSensitive"])) ? $HTTP_POST_VARS["caseSensitive"] : "false";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<head>
<title>Substr Count Test</title>
<style type="text/css">
<!--
td, input, textarea { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; color: #333333}
-->
</style>
</head>
<body bgcolor="#FFFFFF">
<form name="myform" method="post" action="<?php echo basename($PHP_SELF); ?>">
<table width="420" border="1" cellspacing="0" cellpadding="2" bordercolor="#e1e1e1" bordercolordark="#ffffff" align="center">
<tr bgcolor="#CCCC99">
<td height="33" bgcolor="#CCCC99"><b>Parameter</b></td>
<td width="50%"><b>Value</b></td>
</tr>
<tr align="left" valign="middle">
<td>The full string</td>
<td width="50%">
<p>
<textarea name="fullString" cols="24" rows="4" wrap="PHYSICAL"><?= $fullString ?></textarea>
<br>
<br>
<i>Delimiter </i><br>
<input type="text" name="stringToSearch2" size="24" value="<?= $fullStringDelim ?>">
<br>
(Defaults to space)</p>
</td>
</tr>
<tr align="left" valign="middle">
<td>The string to look for</td>
<td width="50%">
<input type="text" name="stringToSearch" size="24" value="<?= $stringToSearch ?>">
</td>
</tr>
<tr align="left" valign="middle">
<td><br>
Should we look for exact word?</td>
<td width="50%">
<input type="text" name="exactMatch" size="24" value="<?= $exactMatch ?>">
</td>
</tr>
<tr align="left" valign="middle">
<td>Case sensitive?</td>
<td width="50%">
<input type="text" name="caseSensitive" size="24" value="<?= $caseSensitive ?>">
</td>
</tr>
<tr align="left" valign="middle">
<td> </td>
<td width="50%">
<input type="submit" name="Submit" value="Count now">
<br>
</td>
</tr>
<tr align="left" valign="middle" bgcolor="#efefef">
<td><font color="#900000"><b>Substr count<br>
</b></font>
<p>(This is similar to <b>substr_count</b>() function in the new PHP versions,
but more flexible, IMHO...)
</td>
<td width="50%" align="center" valign="middle">
<table width="10" border="1" cellspacing="0" cellpadding="3" height="10" bordercolor="#000000" bgcolor="#FFCC33">
<tr>
<td>
<?= substrCount($fullString, $fullStringDelim, $stringToSearch, $exactMatch, $caseSensitive) ?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<font face=verdana size=1><b><br>
</b> <br>
<br>
<p><br>
</font>
</body>
-----------------END OF CODE-----------------