Count occurrences of a word in string: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(Created page with "Count occurrences of a word in string == Excel == Using the function [https://support.office.com/zh-tw/article/substitute-%E5%87%BD%E6%95%B8-6434944e-a904-4336-a9b0-1e58df3bc...")
 
(+ PHP)
Line 12: Line 12:
SELECT FLOOR((LENGTH(@paragraph) - LENGTH(REPLACE(@paragraph, @term, ''))) / LENGTH(@term)) AS occurrences;
SELECT FLOOR((LENGTH(@paragraph) - LENGTH(REPLACE(@paragraph, @term, ''))) / LENGTH(@term)) AS occurrences;
</pre>
</pre>
== PHP ==
Using the [https://www.php.net/manual/en/function.mb-substr-count.php mb_substr_count] ('''binary safe''') or [https://www.php.net/manual/en/function.substr-count.php substr_count] function


[[Category:Software]] [[Category:Programming]] [[Category:Data Science]] [[Category:Text file processing]]
[[Category:Software]] [[Category:Programming]] [[Category:Data Science]] [[Category:Text file processing]]

Revision as of 20:30, 7 August 2019

Count occurrences of a word in string

Excel

Using the function SUBSTITUTE & LEN functions. demo

MySQL way

SET @paragraph := 'an apple a day keeps the doctor away';
SET @term := 'apple';

SELECT FLOOR((LENGTH(@paragraph) - LENGTH(REPLACE(@paragraph, @term, ''))) / LENGTH(@term)) AS occurrences;

PHP

Using the mb_substr_count (binary safe) or substr_count function