Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

php

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);
print_r($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

like image 800
kurry Avatar asked Nov 28 '13 12:11

kurry


2 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);
print_r($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:

\b\w|(?<=\p{Ll})\p{Lu}

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

like image 58
Ibrahim Najjar Avatar answered Oct 29 '22 21:10

Ibrahim Najjar


Can you try this,

    $language = 'Sample Language';

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

    foreach ($language as $value) {
        echo $firstLetter = $value[0];
    }
like image 21
Krish R Avatar answered Oct 29 '22 21:10

Krish R