Count occurrences of a word in string: Difference between revisions
Jump to navigation
Jump to search
m
→PHP
m (→BASH) |
m (→PHP) |
||
Line 33: | Line 33: | ||
== PHP == | == 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] functions. See details on [http://sandbox.onlinephpfunctions.com/code/ | 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] functions. See details on [http://sandbox.onlinephpfunctions.com/code/0b72da5f00701341c77623889d177750242cbb7a demo]. | ||
<pre> | <pre> | ||
<?php | |||
$input = 'an apple a day keeps the doctor away'; | $input = 'an apple a day keeps the doctor away'; | ||
$term = 'apple'; | $term = 'apple'; | ||
echo substr_count($input, $term); | echo substr_count($input, $term) . PHP_EOL; | ||
$input = '一天一蘋果,醫生遠離我'; | |||
$term = '蘋果'; | |||
echo mb_substr_count($input, $term, 'UTF-8') . PHP_EOL; | |||
</pre> | </pre> | ||