src/Manipulator.php
public function nthWord(int $nth, \Closure $closure, bool $preserveSpaces = true) : Manipulator
    {
        $count = 1;
        $modifiedString = '';

        foreach (explode(' ', $this->string) as $word) {
            $modifiedString .= $count === $nth ? $closure($word) : $word;
            $modifiedString .= $preserveSpaces ? ' ' : '';
            $count++;
        }

        return new static(trim($modifiedString));
    }
public function nthWord(int $nth, \Closure $closure, bool $preserveSpaces = true) : Manipulator
    {
        $count = 1;
        $modifiedString = '';

        foreach (explode(' ', $this->string) as $word) {
            $modifiedString .= $count === $nth ? $closure($word) : $word;
            $modifiedString .= $preserveSpaces ? ' ' : '';
            $count++;
        }

        return new static(trim($modifiedString));
    }
public function nthWord ( int $ nth , \ Closure $ closure , bool $ preserveSpaces = true ) : Manipulator { $ count = 1 ; $ modifiedString = '' ; foreach ( explode ( ' ' , $ this -> string ) as $ word ) { $ modifiedString .= $ count === $ nth ? $ closure ( $ word ) : $ word ; $ modifiedString .= $ preserveSpaces ? ' ' : '' ; $ count ++ ; } return new static ( trim ( $ modifiedString ) ) ; }
Perform an action on the nth word.

@param  int
@param  Closure
@param  boolean
@return object|Manipulator
Perform an action on the nth word .