Count occurrences of a word in string: Difference between revisions

Jump to navigation Jump to search
m
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/326ff9f24ebfea9f67ddf2f1475a6172cdcd2e66 demo].
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>


Anonymous user

Navigation menu