Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build Acronyms of a phrase in PHP

I'm looking for a way that I can extract the first letter of each word from an input field and place it into a variable.

Example: if the input field is "Stack-Overflow Questions Tags Users" then the output for the variable should be something like "SOQTU"

like image 557
dmschenk Avatar asked Sep 09 '10 14:09

dmschenk


People also ask

How do you abbreviate phrases?

An abbreviation is a shortened form of a word or phrase; abbreviations of phrases are often composed of the first letter of each word of the phrase (i.e., acronym). To maximize clarity, use abbreviations sparingly. Also consider readers' familiarity with the abbreviation before using it.

Can an acronym be a phrase?

An acronym is a pronounceable word formed from the first letter (or first few letters) of each word in a phrase or title.


1 Answers

$s = 'Stack-Overflow Questions Tags Users';
echo preg_replace('/\b(\w)|./', '$1', $s);

the same as codaddict's but shorter

  • For unicode support, add the u modifier to regex: preg_replace('...../u',
like image 78
user187291 Avatar answered Oct 05 '22 04:10

user187291