如何在PHP中使用正则表达式获取每个单词的首字母 [英] How to get first letter of every word using regex in 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);
我得到的只是第一个单词的第一个字母.上面打印 Array([0]=>S [1]=>S)
What I am getting is only the first letter of the first word. The above prints Array([0]=>S [1]=>S)
如果我改成
preg_match('/(\bL)+/i', $language, $match);
我可以匹配第二个单词的 L.所以很明显,在第一场比赛之后,它停止寻找其余的比赛.我不太擅长正则表达式.谁能指出我做错了什么?
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?
输入字符串中的单词并不总是用空格分隔.这是相当不可预测的.我遇到的一些格式:主要语言:语言"主要语言,语言"MainLanguage:Language-SubLanguage"这里我想得到M,L和S
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
推荐答案
首先使用 preg_match_all
来解决这个问题,其次你不需要 +
量词:
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
:匹配一个词边界,词边界是一个词字符与一个非词字符分开的位置.单词字符通常是[a-zA-Z0-9_]
.\w
:匹配一个单词字符.\b\w
匹配位于单词边界位置的单词字符,在这种情况下,紧跟在分隔单词的空格或字符串开头之后.\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}
表达式的第二部分,即 (?<=\w)\p{Lu}
应该匹配任何单词字符,如果它是大写字符 \p{Lu}
后面的小写字母 \p{Ll}
应该涵盖驼峰情况,原始表达式涵盖使用连字符 -
分隔的情况两个字.
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.
这篇关于如何在PHP中使用正则表达式获取每个单词的首字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!