I have Below Paragraph. I want to Find Number of Dashes araises in given paragraph??
1.I opened ______ door and found ______ old man wearing ______ hat standing on _____ doorstep.
Is Possible??
You should use substr_count
Just declare your text in a variable and count it
<?php
$text = "1.I opened ______ door and found ______ old man wearing ______ hat standing on _____ doorstep.";
echo substr_count($text,"_______");
?>
Note : It will count the number of _____
. [Dashes _____
is 5
in count]
Here is the demo
Ok so the OP wants to count the number of words to be filled in represented by a series of underscores.
The number of underscores can also vary, I'm guessing to represent the length of the word.
So a more generic solution would be... Set the regex for the desired pattern
$search_string = '/[^_]?[_]+[^_]?/'; // Looking for one or more '_' sequences
A simpler version would be
$search_string = '/[_]{2,}/'; // Looking for at least 2 '_' sequences
Perform the blank word count where needed.
$word_count = preg_match_all($search_string,$text);
echo $word_count; // Just show the count for testing
The regex is pretty generic but should work for the above cases.
Additional: You are much better off creating a function to implement this.
function count_blank_words($text){
$search_string = '/[_]{2,}/'; // Looking for at least 2 '_' sequences
return preg_match_all($search_string,$text);
}
and call it using
echo count_blank_words($text);