Home » Php » How to get first letter of every word using regex in PHP

How to get first letter of every word using regex in PHP

Posted by: admin July 12, 2020 Leave a comment


I have a string variable and I want to get the first letter of every word of it.
I want the end result to be an array of first letters.

$language = 'Sample Language';
preg_match('/(\b[a-z])+/i', $language, $match);

What I am getting is only the first letter of the first word.
The above prints Array([0]=>S [1]=>S)

If I change it to

preg_match('/(\bL)+/i', $language, $match);

I can get a match for the L of the second word. So obviously after the first match it stops looking for the rest. I am not very good with regular expressions. Can anyone point out what I have done wrong?

words in input string are not always separated by space. It is quite unpredictable. Some formats I have come across:
“MainLanguage: Language”
“MainLanguage, Language”
“MainLanguage: Language-SubLanguage” here I want to get M, L and S

How to&Answers:

First of all use preg_match_all for this, and secondly you don’t need the + quantifier:

$language = 'Sample Language';
preg_match_all('/\b\w/', $language, $match);
  • \b: Matches a word boundary, a word boundary is a position that separates a word character from a non-word character. Word characters are usually [a-zA-Z0-9_].
  • \w: Matches a word character.
  • \b\w Matches a word character that is at a word boundary position, in this case right after the space or the start-of-string that separates the words.

In case you want camel case situations then you can combine the previous expression with another one like this:


The second part of the expression, namely (?<=\w)\p{Lu} should match any word character if it is an uppercase character \p{Lu} following a lowercase one \p{Ll} which should cover the camel case situation, the original expressions covers the situation when a hyphen - is used to separate the two words.

Regex101 Demo


Can you try this,

    $language = 'Sample Language';

    $language = explode(" ", $language);

    foreach ($language as $value) {
        echo $firstLetter = $value[0];