开发者

php str_word_count

I am trying to count the number words in paragraphs of texts. Right now I'm usi开发者_Python百科ng str_word_count, which is going great except the single quotation marks are messing things up.

The function is counting 'propter' and propter as different words. It is also counting et' and et, 'non and non as different words.

How can I get the function to ignore the single quotations. The text is all in latin so there are no instances of single quotations within words (if that makes any difference).


You can always remove the single quotation marks before using the str_word_count().


Slightly hacky solution, but you should be able to do:

$count = str_word_count(str_replace("'",'',$str));

...and get the answer you want, given the example problems above.

EDIT

If you are trying to get the number of unique words in the passage, as suggested by Pablo M. Prieto, you can do it like this:

$count = count(array_unique(str_word_count(strtolower(str_replace("'",'',$str)),1)));


Actually, that is the default behavior of str_word_count() function. It counts the amount of words in the paragraph.

So if you have:

$paragraph = "Lorem ipsum dolor sit amet 'lorem' ipsum 'dolor'";

and

$paragraph = "Lorem ipsum dolor sit amet lorem ipsum dolor";

it will return int(8) for both of them.

Were you trying to count the amount of unique words in the paragraph?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜