Tuesday 16 December 2014

Get a sentence,string initials or abbreviation via Regular Expression

Please use the below function to get the sentence/string initial (abbreviation).

function initials($str) {
        $matches = preg_split("/[,]/", $str);

        $initials = array();
        foreach ($matches as $list) {
            $words = preg_split("/[^\w-]/", ($list));

            $initial = '';
            foreach ($words as $word) {
                $initial .= strtoupper($word[0]);
            }
            $initials[] = $initial;
        }

        return ( implode('/', $initials) );
    }

Also, please use the link to understand more about regular expression using this Regular Expression Tutorials

No comments:

Post a Comment

Please post any queries and comments here.