src/Initials.php
private function generateInitials()
    {
	    $nameOrInitials = trim($this->name);

    	if( !$this->keepCase ) {
		    $nameOrInitials = mb_strtoupper($nameOrInitials);
	    }

        $names = explode(' ', $nameOrInitials);
        $initials = $nameOrInitials;
        $assignedNames = 0;

        if (count($names) > 1) {
            $initials = '';
            $start = 0;

            for ($i = 0; $i < $this->length; $i++) {
                $index = $i;

                if (($index === ($this->length - 1) && $index > 0) || ($index > (count($names) - 1))) {
                    $index = count($names) - 1;
                }

                if ($assignedNames >= count($names)) {
                    $start++;
                }

                $initials .= mb_substr($names[$index], $start, 1);
                $assignedNames++;
            }
        }

        $initials = mb_substr($initials, 0, $this->length);

        return $initials;
    }
private function generateInitials()
    {
	    $nameOrInitials = trim($this->name);

    	if( !$this->keepCase ) {
		    $nameOrInitials = mb_strtoupper($nameOrInitials);
	    }

        $names = explode(' ', $nameOrInitials);
        $initials = $nameOrInitials;
        $assignedNames = 0;

        if (count($names) > 1) {
            $initials = '';
            $start = 0;

            for ($i = 0; $i < $this->length; $i++) {
                $index = $i;

                if (($index === ($this->length - 1) && $index > 0) || ($index > (count($names) - 1))) {
                    $index = count($names) - 1;
                }

                if ($assignedNames >= count($names)) {
                    $start++;
                }

                $initials .= mb_substr($names[$index], $start, 1);
                $assignedNames++;
            }
        }

        $initials = mb_substr($initials, 0, $this->length);

        return $initials;
    }
private function generateInitials ( ) { $ nameOrInitials = trim ( $ this -> name ) ; if ( ! $ this -> keepCase ) { $ nameOrInitials = mb_strtoupper ( $ nameOrInitials ) ; } $ names = explode ( ' ' , $ nameOrInitials ) ; $ initials = $ nameOrInitials ; $ assignedNames = 0 ; if ( count ( $ names ) > 1 ) { $ initials = '' ; $ start = 0 ; for ( $ i = 0 ; $ i < $ this -> length ; $ i ++ ) { $ index = $ i ; if ( ( $ index === ( $ this -> length - 1 ) && $ index > 0 ) || ( $ index > ( count ( $ names ) - 1 ) ) ) { $ index = count ( $ names ) - 1 ; } if ( $ assignedNames >= count ( $ names ) ) { $ start ++ ; } $ initials .= mb_substr ( $ names [ $ index ] , $ start , 1 ) ; $ assignedNames ++ ; } } $ initials = mb_substr ( $ initials , 0 , $ this -> length ) ; return $ initials ; }
Generate a two-letter initial from a name,
and if no name, assume its already initials.
For safety, we limit it to two characters,
in case its a single, but long, name.

@return string
Generate a two - letter initial from a name and if no name assume its already initials . For safety we limit it to two characters in case its a single but long name .