I'm trying to count words in a sample string like this:
<p> <p>hello world!</p><p> </p></p>
after reading the documentation, I found a function which is supposed to do exactly what I'm trying to do. but somehow the result is not quite right.
this is the code I'm using:
function rip_tags($string) {
// ----- remove HTML TAGs -----
$string = preg_replace ('/<[^>]*>/', ' ', $string);
// ----- remove control characters -----
$string = str_replace("\r", '', $string); // --- replace with empty space
$string = str_replace("\n", ' ', $string); // --- replace with space
$string = str_replace("\t", ' ', $string); // --- replace with space
// ----- remove multiple spaces -----
$string = trim(preg_replace('/ {2,}/', ' ', $string));
return $string;
}
$str = '<p> <p>hello world!</p><p> </p></p>';
$str = trim(html_entity_decode($str));
$str = rip_tags($str);
$c = str_word_count($str);
echo $c;
the result should've been 2 but the code returns 4.. what am I missing??