Swat/SwatString.php
public static function condenseToName($string, $max_length = 15)
    {
        if (!is_string($string)) {
            $string = strval($string);
        }

        if ($string == '') {
            return $string;
        }

        // remove tags and make lowercase
        $string = strip_tags(mb_strtolower($string));

        if (class_exists('Net_IDNA')) {
            // we have Net_IDNA, convert words to punycode

            // convert entities to utf-8
            $string = self::minimizeEntities($string);

            // convert non-alpha-numeric ascii characters to spaces and
            // condense whitespace
            $search = array(
                '/[\x00-\x1f\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]/u',
                '/\s+/u'
            );
            $replace = array(' ', ' ');
            $string = preg_replace($search, $replace, $string);

            // remove leading and tailing whitespace that may have been added
            // during preg_replace()
            $string = trim($string);

            $idna = Net_IDNA::getInstance();

            // split into words
            $string_utf8_exp = explode(' ', $string);

            // convert words into punycode
            $first = true;
            $string_out = '';
            foreach ($string_utf8_exp as $string_utf8) {
                $encoded_word = $idna->encode($string_utf8);

                if ($first) {
                    // first word too long, so forced to chop it
                    if (mb_strlen($encoded_word) >= $max_length) {
                        return mb_substr($encoded_word, 0, $max_length);
                    }

                    $first = false;
                }

                // this word would push us over the limit
                $new_length = mb_strlen($string_out) + mb_strlen($encoded_word);
                if ($new_length > $max_length) {
                    return $string_out;
                }

                $string_out .= $encoded_word;
            }
        } else {
            // remove html entities, convert non-alpha-numeric characters to
            // spaces and condense whitespace
            $search = array('/&#?\w+;/u', '/[^a-z0-9 ]/u', '/\s+/u');
            $replace = array('', ' ', ' ');

            $string = preg_replace($search, $replace, $string);

            // split into words
            $string_exp = explode(' ', $string);

            // first word too long, so forced to chop it
            if (mb_strlen($string_exp[0]) >= $max_length) {
                return mb_substr($string_exp[0], 0, $max_length);
            }

            $string_out = '';

            // add words to output until it is too long
            foreach ($string_exp as $word) {
                // this word would push us over the limit
                if (mb_strlen($string_out) + mb_strlen($word) > $max_length) {
                    return $string_out;
                }

                $string_out .= $word;
            }
        }

        return $string_out;
    }
public static function condenseToName($string, $max_length = 15)
    {
        if (!is_string($string)) {
            $string = strval($string);
        }

        if ($string == '') {
            return $string;
        }

        // remove tags and make lowercase
        $string = strip_tags(mb_strtolower($string));

        if (class_exists('Net_IDNA')) {
            // we have Net_IDNA, convert words to punycode

            // convert entities to utf-8
            $string = self::minimizeEntities($string);

            // convert non-alpha-numeric ascii characters to spaces and
            // condense whitespace
            $search = array(
                '/[\x00-\x1f\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]/u',
                '/\s+/u'
            );
            $replace = array(' ', ' ');
            $string = preg_replace($search, $replace, $string);

            // remove leading and tailing whitespace that may have been added
            // during preg_replace()
            $string = trim($string);

            $idna = Net_IDNA::getInstance();

            // split into words
            $string_utf8_exp = explode(' ', $string);

            // convert words into punycode
            $first = true;
            $string_out = '';
            foreach ($string_utf8_exp as $string_utf8) {
                $encoded_word = $idna->encode($string_utf8);

                if ($first) {
                    // first word too long, so forced to chop it
                    if (mb_strlen($encoded_word) >= $max_length) {
                        return mb_substr($encoded_word, 0, $max_length);
                    }

                    $first = false;
                }

                // this word would push us over the limit
                $new_length = mb_strlen($string_out) + mb_strlen($encoded_word);
                if ($new_length > $max_length) {
                    return $string_out;
                }

                $string_out .= $encoded_word;
            }
        } else {
            // remove html entities, convert non-alpha-numeric characters to
            // spaces and condense whitespace
            $search = array('/&#?\w+;/u', '/[^a-z0-9 ]/u', '/\s+/u');
            $replace = array('', ' ', ' ');

            $string = preg_replace($search, $replace, $string);

            // split into words
            $string_exp = explode(' ', $string);

            // first word too long, so forced to chop it
            if (mb_strlen($string_exp[0]) >= $max_length) {
                return mb_substr($string_exp[0], 0, $max_length);
            }

            $string_out = '';

            // add words to output until it is too long
            foreach ($string_exp as $word) {
                // this word would push us over the limit
                if (mb_strlen($string_out) + mb_strlen($word) > $max_length) {
                    return $string_out;
                }

                $string_out .= $word;
            }
        }

        return $string_out;
    }
public static function condenseToName ( $ string , $ max_length = 15 ) { if ( ! is_string ( $ string ) ) { $ string = strval ( $ string ) ; } if ( $ string == '' ) { return $ string ; } // remove tags and make lowercase $ string = strip_tags ( mb_strtolower ( $ string ) ) ; if ( class_exists ( 'Net_IDNA' ) ) { // we have Net_IDNA, convert words to punycode // convert entities to utf-8 $ string = self :: minimizeEntities ( $ string ) ; // convert non-alpha-numeric ascii characters to spaces and // condense whitespace $ search = array ( '/[\x00-\x1f\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]/u' , '/\s+/u' ) ; $ replace = array ( ' ' , ' ' ) ; $ string = preg_replace ( $ search , $ replace , $ string ) ; // remove leading and tailing whitespace that may have been added // during preg_replace() $ string = trim ( $ string ) ; $ idna = Net_IDNA :: getInstance ( ) ; // split into words $ string_utf8_exp = explode ( ' ' , $ string ) ; // convert words into punycode $ first = true ; $ string_out = '' ; foreach ( $ string_utf8_exp as $ string_utf8 ) { $ encoded_word = $ idna -> encode ( $ string_utf8 ) ; if ( $ first ) { // first word too long, so forced to chop it if ( mb_strlen ( $ encoded_word ) >= $ max_length ) { return mb_substr ( $ encoded_word , 0 , $ max_length ) ; } $ first = false ; } // this word would push us over the limit $ new_length = mb_strlen ( $ string_out ) + mb_strlen ( $ encoded_word ) ; if ( $ new_length > $ max_length ) { return $ string_out ; } $ string_out .= $ encoded_word ; } } else { // remove html entities, convert non-alpha-numeric characters to // spaces and condense whitespace $ search = array ( '/&#?\w+;/u' , '/[^a-z0-9 ]/u' , '/\s+/u' ) ; $ replace = array ( '' , ' ' , ' ' ) ; $ string = preg_replace ( $ search , $ replace , $ string ) ; // split into words $ string_exp = explode ( ' ' , $ string ) ; // first word too long, so forced to chop it if ( mb_strlen ( $ string_exp [ 0 ] ) >= $ max_length ) { return mb_substr ( $ string_exp [ 0 ] , 0 , $ max_length ) ; } $ string_out = '' ; // add words to output until it is too long foreach ( $ string_exp as $ word ) { // this word would push us over the limit if ( mb_strlen ( $ string_out ) + mb_strlen ( $ word ) > $ max_length ) { return $ string_out ; } $ string_out .= $ word ; } } return $ string_out ; }
Condenses a string to a name

The generated name can be used for things like database identifiers and
site URI fragments.

Example:
<code>
$string = 'The quick brown fox jumped over the lazy dogs.';
// displays 'thequickbrown'
echo SwatString::condenseToName($string);
</code>

@param string $string the string to condense to a name.
@param integer $max_length the maximum length of the condensed name in
characters.

@return string the string condensed into a name.
Condenses a string to a name