src/Util/Str.php
public static function human($str)
    {
        // Cleanup
        $out = str_replace('_', ' ', $str);
        $out = ucwords(strtolower($out));
        $out = preg_replace('/\s{2,}/', ' ', $out);
        $out = preg_replace('/^\s/', '', $out);
        $out = preg_replace('/\s$/', '', $out);
        if (strlen($out) === 0) {
            return $out;
        }

        // Gather stopwords before looping
        $stop_words_lower = self::stopWordsLower();

        // Handle each word
        $words = explode(' ', $out);
        $out_words = array();
        foreach ($words as $n => $word) {
            $out_word = $word;

            // If we have a special match, don't do anything else
            $specials = array(
                '/^id$/i'   => 'ID',
                '/^ids$/i'  => 'IDs',
                '/^url$/i'  => 'URL',
                '/^urls$/i' => 'URLs',
                '/^cta$/i'  => 'CTA',
                '/^api$/i'  => 'API',
                '/^faq$/i'  => 'FAQ',
                '/^ip$/i'   => 'IP',
                '/^why$/'   => 'why',
                '/^Why$/'   => 'Why',
            );
            $special_word = false;
            foreach ($specials as $regex => $special) {
                if (!preg_match($regex, $word)) {
                    continue;
                }

                $special_word = true;
                $out_word = $special;
            }
            if ($special_word) {
                $out_words[] = $out_word;
                continue;
            }

            // Handle acronyms without vowels
            if (!preg_match('/[aeiou]/i', $word)) {
                $out_word = strtoupper($out_word);
            }

            // Stop words
            $lower = strtolower($word);
            if (in_array($lower, $stop_words_lower) && $n !== 0) {
                $out_word = $lower;
            }

            $out_words[] = $out_word;
        }
        $out = join(' ', $out_words);

        // Questions
        $first_word_lower = strtolower($words[0]);
        $first_word_lower_no_contraction = preg_replace("/'s$/", '', $first_word_lower);
        $is_question = in_array($first_word_lower_no_contraction, self::questionWords());
        $has_question_mark = (bool) preg_match('/\?+$/', $out);
        if ($is_question && !$has_question_mark) {
            $out .= '?';
        }

        return $out;
    }
public static function human($str)
    {
        // Cleanup
        $out = str_replace('_', ' ', $str);
        $out = ucwords(strtolower($out));
        $out = preg_replace('/\s{2,}/', ' ', $out);
        $out = preg_replace('/^\s/', '', $out);
        $out = preg_replace('/\s$/', '', $out);
        if (strlen($out) === 0) {
            return $out;
        }

        // Gather stopwords before looping
        $stop_words_lower = self::stopWordsLower();

        // Handle each word
        $words = explode(' ', $out);
        $out_words = array();
        foreach ($words as $n => $word) {
            $out_word = $word;

            // If we have a special match, don't do anything else
            $specials = array(
                '/^id$/i'   => 'ID',
                '/^ids$/i'  => 'IDs',
                '/^url$/i'  => 'URL',
                '/^urls$/i' => 'URLs',
                '/^cta$/i'  => 'CTA',
                '/^api$/i'  => 'API',
                '/^faq$/i'  => 'FAQ',
                '/^ip$/i'   => 'IP',
                '/^why$/'   => 'why',
                '/^Why$/'   => 'Why',
            );
            $special_word = false;
            foreach ($specials as $regex => $special) {
                if (!preg_match($regex, $word)) {
                    continue;
                }

                $special_word = true;
                $out_word = $special;
            }
            if ($special_word) {
                $out_words[] = $out_word;
                continue;
            }

            // Handle acronyms without vowels
            if (!preg_match('/[aeiou]/i', $word)) {
                $out_word = strtoupper($out_word);
            }

            // Stop words
            $lower = strtolower($word);
            if (in_array($lower, $stop_words_lower) && $n !== 0) {
                $out_word = $lower;
            }

            $out_words[] = $out_word;
        }
        $out = join(' ', $out_words);

        // Questions
        $first_word_lower = strtolower($words[0]);
        $first_word_lower_no_contraction = preg_replace("/'s$/", '', $first_word_lower);
        $is_question = in_array($first_word_lower_no_contraction, self::questionWords());
        $has_question_mark = (bool) preg_match('/\?+$/', $out);
        if ($is_question && !$has_question_mark) {
            $out .= '?';
        }

        return $out;
    }
public static function human ( $ str ) { // Cleanup $ out = str_replace ( '_' , ' ' , $ str ) ; $ out = ucwords ( strtolower ( $ out ) ) ; $ out = preg_replace ( '/\s{2,}/' , ' ' , $ out ) ; $ out = preg_replace ( '/^\s/' , '' , $ out ) ; $ out = preg_replace ( '/\s$/' , '' , $ out ) ; if ( strlen ( $ out ) === 0 ) { return $ out ; } // Gather stopwords before looping $ stop_words_lower = self :: stopWordsLower ( ) ; // Handle each word $ words = explode ( ' ' , $ out ) ; $ out_words = array ( ) ; foreach ( $ words as $ n => $ word ) { $ out_word = $ word ; // If we have a special match, don't do anything else $ specials = array ( '/^id$/i' => 'ID' , '/^ids$/i' => 'IDs' , '/^url$/i' => 'URL' , '/^urls$/i' => 'URLs' , '/^cta$/i' => 'CTA' , '/^api$/i' => 'API' , '/^faq$/i' => 'FAQ' , '/^ip$/i' => 'IP' , '/^why$/' => 'why' , '/^Why$/' => 'Why' , ) ; $ special_word = false ; foreach ( $ specials as $ regex => $ special ) { if ( ! preg_match ( $ regex , $ word ) ) { continue ; } $ special_word = true ; $ out_word = $ special ; } if ( $ special_word ) { $ out_words [ ] = $ out_word ; continue ; } // Handle acronyms without vowels if ( ! preg_match ( '/[aeiou]/i' , $ word ) ) { $ out_word = strtoupper ( $ out_word ) ; } // Stop words $ lower = strtolower ( $ word ) ; if ( in_array ( $ lower , $ stop_words_lower ) && $ n !== 0 ) { $ out_word = $ lower ; } $ out_words [ ] = $ out_word ; } $ out = join ( ' ' , $ out_words ) ; // Questions $ first_word_lower = strtolower ( $ words [ 0 ] ) ; $ first_word_lower_no_contraction = preg_replace ( "/'s$/" , '' , $ first_word_lower ) ; $ is_question = in_array ( $ first_word_lower_no_contraction , self :: questionWords ( ) ) ; $ has_question_mark = ( bool ) preg_match ( '/\?+$/' , $ out ) ; if ( $ is_question && ! $ has_question_mark ) { $ out .= '?' ; } return $ out ; }
Humanize the string
@param string $str
@return string
Humanize the string