note 85592 deleted from function.str-word-count by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 16:25:16 +0000 |
| Subject: | note 85592 deleted from function.str-word-count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190361@lists.php.net to get a copy of this message | ||
Note Submitter: om+www dot php dot net at miakinen dot net
----
The previous function str_word_count_utf8 implemented only the first parameter, $string. Here is an
implementation which also supports the second parameter, $format. The $charlist is not supported,
though it could be possible to give the possibility to change the MASK.
<?php
define("WORD_COUNT_MASK", "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u");
function str_word_count_utf8($string, $format = 0)
{
switch ($format) {
case 1:
preg_match_all(WORD_COUNT_MASK, $string, $matches);
return $matches[0];
case 2:
preg_match_all(WORD_COUNT_MASK, $string, $matches, PREG_OFFSET_CAPTURE);
$result = array();
foreach ($matches[0] as $match) {
$result[$match[1]] = $match[0];
}
return $result;
}
return preg_match_all(WORD_COUNT_MASK, $string, $matches);
}
?>
Note that in the case $format=2 the numeric positions are expressed in octets, not in characters.