How to build Acronyms of a phrase in PHP

dmschenk picture dmschenk · Sep 9, 2010 · Viewed 8.1k times · Source

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"

Answer

user187291 picture user187291 · Sep 9, 2010
$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',