Webiny\Component\StdLib\StdObject\StringObject\StringObject::wordCount PHP Method

wordCount() public method

Get the number of words in the string.
public wordCount ( integer $format ) : mixed | ArrayObject
$format integer Specify the return format: 0 - return number of words 1 - return an ArrayObject containing all the words found inside the string 2 - returns an ArrayObject, where the key is the numeric position of the word inside the string and the value is the actual word itself
return mixed | Webiny\Component\StdLib\StdObject\ArrayObject\ArrayObject An ArrayObject or integer, based on the wanted $format, with the stats about the words in the string.
    public function wordCount($format = 0)
    {
        if ($format < 1) {
            return str_word_count($this->val(), $format);
        } else {
            return new ArrayObject(str_word_count($this->val(), $format));
        }
    }

Usage Example

Example #1
0
 /**
  * @dataProvider stringSet
  */
 public function testWordCount3($str)
 {
     $s = new StringObject($str);
     $words = $s->wordCount(2);
     $wordCountCheck = str_word_count($str, 2);
     $this->assertSame($wordCountCheck, $words->val());
 }