如何统计长字符串中以英文句点结尾的片段重复次数?
Great question! Let me break down whether this method works, and how to tweak it for accurate results:
Your Core Idea Has Merit, But Needs Key Adjustments
Using explode('.', $str) to split the string is a solid starting point, but relying solely on substr_count() to count repetitions can lead to unexpected errors. Here’s why, and how to fix it:
Problem 1: explode() Removes Periods and Leaves Empty/Unclean Elements
When you split the string with ".", each fragment loses its trailing period. Plus, if your original string ends with a ".", you’ll get an empty string as the final array element. For example, splitting your sample string would generate elements like:
[ "Manual Testing is a type of Software Testing where Testers following are few common myths and facts related to testing", " Manual Testing is the most primitive of all testing types and helps find bugs in the software system", " Manual Testing is a type of Software Testing where Testers manually execute test cases without using any automation tools", " Manual Testing is the most primitive of all testing types and helps find bugs in the software system", "" ]
Notice the leading spaces on some entries and the final empty string—these need cleanup before counting.
Problem 2: substr_count() Can Match Partial Fragments
If you take a split fragment (without the ".") and run substr_count($str, $fragment), it might count instances where the fragment appears as part of a longer sentence. For example, if you have "Hello world." and "Hello world again.", substr_count() would count both, even though they’re not the full repeated sentence you care about.
A Refined, Working Version of Your Approach
Here’s how to adjust your method to get precise results:
Split and Clean the Fragments
Useexplode('.', $str)to split, then filter out empty strings, trim whitespace, and reattach the "." to get complete, clean sentences:$str = "Manual Testing is a type of Software Testing where Testers following are few common myths and facts related to testing. Manual Testing is the most primitive of all testing types and helps find bugs in the software system. Manual Testing is a type of Software Testing where Testers manually execute test cases without using any automation tools. Manual Testing is the most primitive of all testing types and helps find bugs in the software system."; $fragments = explode('.', $str); $cleanSentences = []; foreach ($fragments as $frag) { $trimmed = trim($frag); if (!empty($trimmed)) { $cleanSentences[] = $trimmed . '.'; } }Count Repetitions with
array_count_values()
This function accurately counts how many times each complete sentence appears—far more reliable thansubstr_count()for full sentence matches:$sentenceCounts = array_count_values($cleanSentences);Calculate Total Repeated Occurrences
For each sentence that appears more than once, subtract 1 (since the first occurrence isn’t a repetition) and sum those values:$totalRepetitions = 0; foreach ($sentenceCounts as $count) { if ($count > 1) { $totalRepetitions += ($count - 1); } } echo "There is $totalRepetitions occurrence in your string";Running this with your sample string will output the expected
There is 1 occurrence in your string.
Final Verdict
Your initial approach is on the right track! Swapping substr_count() for array_count_values() (after cleaning the fragments) gives you accurate, reliable results without false positives from partial matches.
内容的提问来源于stack exchange,提问作者Satanas

