Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在str_word_count()中将缩略语计算为2个单词,而不是1个单词_Php - Fatal编程技术网

Php 在str_word_count()中将缩略语计算为2个单词,而不是1个单词

Php 在str_word_count()中将缩略语计算为2个单词,而不是1个单词,php,Php,我试图得到一个字符串的字数,但我想把缩略语计算为2个字,而不是1个字。有没有办法用str_word_count()实现这一点 结果: 二, 想要的结果: 3您可以通过以下方式绕过: $string = "i'm not"; $replacements = array('"', "'"); $count = str_word_count(str_replace($replacements, " ", $string)); echo $count; // output 3 您可以计算出现的撇号数

我试图得到一个字符串的字数,但我想把缩略语计算为2个字,而不是1个字。有没有办法用str_word_count()实现这一点

结果:

二,

想要的结果:


3

您可以通过以下方式绕过:

$string = "i'm not";
$replacements = array('"', "'");
$count = str_word_count(str_replace($replacements, " ", $string));

echo $count; // output 3

您可以计算出现的撇号数量,并将其添加到
str\u word\u count
中的字数中

$string = "i'm not";
$count = str_word_count($string);
$count += substr_count($string, "'");

echo $count;
// $count == 3
正如你在中所看到的,英语中有很多缩略词(有些我以前从未见过,有些已经不用了)。因此,到目前为止,以下建议并非详尽无遗

您可以决定忽略大部分内容,集中精力于
'd
're
'll
-请随意添加更多内容。
然后,对字符串中的单词进行计数(使用
str\u word\u count
)并搜索上述子字符串,每个子字符串加1。

您可以使用
sizeof(explode(“,$string))
对由空格分隔的单词进行计数。然后,您可以执行类似于sizeof(explode(“'”,$string2))的操作来计算这些单词中有多少是缩略词。显然,这种方法的问题是像
我要去我朋友的家
这样的句子会计数为8,而不是我想象中的7,因为
朋友的
会被计算为两个单词,即使它是所有格的。@hobenkr Yeah不会想要(朋友的)计数为2个单词。不幸的是,
Joe's house
的计数将是3而不是2,即使它不等同于
Joe is house
。如果字符串在其他位置包含
,该怎么办?比如“我不‘无聊’”-你的方法会得到6而不是4。@TravelingTechGuy是真的。@frosty在这种情况下。。。你需要加一本字典,里面有英语词汇表上所有的等价词,比如“我”等于“我是”,等等,然后你用“我是”替换每个“我是”,你就可以数数了。否则这是不可能的。@hobenkr当然我们可以试着匹配每一个带有收缩的代词,但那会很痛苦,哈哈。如果字符串在其他地方包含
?比如“我不‘无聊’”——你的方法会得到6而不是4。是的。这是最有效的方法。我也在想类似的事情,但在我的脑海里,我也在为匹配添加代词。忘了我们可以匹配宫缩本身,哈哈。
$string = "i'm not";
$count = str_word_count($string);
$count += substr_count($string, "'");

echo $count;
// $count == 3