random1 Posted October 13, 2008 Share Posted October 13, 2008 How can you convert the text (say 'National Aeronautics and Space Administration') into the abbreviation 'NASA' using a PHP function? (Logic may be get each first capital letter?) Link to comment https://forums.phpfreaks.com/topic/128282-solved-convert-text-into-abbreviation/ Share on other sites More sharing options...
Lamez Posted October 13, 2008 Share Posted October 13, 2008 preg_replace_callback() Link to comment https://forums.phpfreaks.com/topic/128282-solved-convert-text-into-abbreviation/#findComment-664495 Share on other sites More sharing options...
Zane Posted October 13, 2008 Share Posted October 13, 2008 They're actually called acronyms, but oh well $keywords = preg_split( "/([^A-Z])/", "High-Performance Instrumented Airborne Platform for Environmental Research", 0); echo implode("", $keywords); Link to comment https://forums.phpfreaks.com/topic/128282-solved-convert-text-into-abbreviation/#findComment-664498 Share on other sites More sharing options...
Barand Posted October 13, 2008 Share Posted October 13, 2008 $str = 'National Aeronautics and Space Administration'; $k = strlen($str); $abbrev = ''; for ($i=0; $i < $k; $i++) { $c = $str[$i]; if ('A' <= $c && $c <= 'Z') $abbrev .= $c; } echo $abbrev; Link to comment https://forums.phpfreaks.com/topic/128282-solved-convert-text-into-abbreviation/#findComment-664502 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 They're actually called acronyms, but oh well $keywords = preg_split( "/([^A-Z])/", "High-Performance Instrumented Airborne Platform for Environmental Research", 0); echo implode("", $keywords); Nice! Link to comment https://forums.phpfreaks.com/topic/128282-solved-convert-text-into-abbreviation/#findComment-664503 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 Actually, it works even better with the PREG_SPLIT_NO_EMPTY flag: <?php $keywords = preg_split( "/([^A-Z])/", "High-Performance Instrumented Airborne Platform for Environmental Research", -1, PREG_SPLIT_NO_EMPTY); echo implode("", $keywords); Shortens $keywords by a LOT. Link to comment https://forums.phpfreaks.com/topic/128282-solved-convert-text-into-abbreviation/#findComment-664513 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.