如何获得给定字符串的每个单词的第一个字母?
$string = "Community College District";
$result = "CCD";
Run Code Online (Sandbox Code Playgroud)
我发现了javascript方法,但不知道如何将其转换为php.
Mic*_*ski 119
explode()在空格上,然后使用[]符号将结果字符串作为数组访问:
$words = explode(" ", "Community College District");
$acronym = "";
foreach ($words as $w) {
$acronym .= $w[0];
}
Run Code Online (Sandbox Code Playgroud)
如果您期望多个空格可能会分隔单词,请切换为 preg_split()
$words = preg_split("/\s+/", "Community College District");
Run Code Online (Sandbox Code Playgroud)
或者,如果除了空格之外的字符分隔单词(-,_),也可以使用preg_split():
// Delimit by multiple spaces, hyphen, underscore, comma
$words = preg_split("/[\s,_-]+/", "Community College District");
Run Code Online (Sandbox Code Playgroud)
Dav*_*dom 41
完成此任务的最佳方法是使用正则表达式.
让我们以合乎逻辑的方式分解你想要的东西:你希望字符串中的每个字符都在一个单词的开头.识别这些字符的最佳方法是查找以空格开头的字符.
所以我们从该空间角色的lookbehind开始,然后是任何角色:
/(?<=\s)./
Run Code Online (Sandbox Code Playgroud)
这将找到任何以空格开头的字符.但是 - 字符串中的第一个字符是字符串中的字符是您想要提取的字符.因为它是字符串中的第一个字符,所以它不能以空格开头.所以我们希望匹配前面有空格或字符串中第一个字符的任何内容,所以我们添加一个主题开始的断言:
/(?<=\s|^)./
Run Code Online (Sandbox Code Playgroud)
现在我们越来越近了.但是如果字符串包含多个空格的块呢?如果它包含一个后跟标点字符的空格怎么办?我们可能不想匹配任何这些,我们可能只想匹配字母.我们可以用字符类 来做到这一点[a-zA-Z].我们可以使用i 修饰符使表达式不区分大小写.
所以我们最终得到:
/(?<=\s|^)[a-z]/i
Run Code Online (Sandbox Code Playgroud)
但是我们如何在PHP中实际使用它呢?好吧,我们希望匹配字符串中所有出现的正则表达式,以便我们使用(你猜对了)preg_match_all():
$string = "Progress in Veterinary Science";
$expr = '/(?<=\s|^)[a-z]/i';
preg_match_all($expr, $string, $matches);
Run Code Online (Sandbox Code Playgroud)
现在我们拥有了我们想要提取的所有角色.要构造您显示的结果字符串,我们需要再次将它们连接在一起:
$result = implode('', $matches[0]);
Run Code Online (Sandbox Code Playgroud)
......我们需要确保它们都是大写的:
$result = strtoupper($result);
Run Code Online (Sandbox Code Playgroud)
这就是它的全部内容.
cas*_*raf 16
假设这些单词都被空格分割,这是一个合适的解决方案:
$string = "Progress in Veterinary Science";
function initials($str) {
$ret = '';
foreach (explode(' ', $str) as $word)
$ret .= strtoupper($word[0]);
return $ret;
}
echo initials($string); // would output "PIVS"
Run Code Online (Sandbox Code Playgroud)
有很多explode答案.我认为使用该strtok功能是一个更优雅和内存效率更高的解决方案:
function createAcronym($string) {
$output = null;
$token = strtok($string, ' ');
while ($token !== false) {
$output .= $token[0];
$token = strtok(' ');
}
return $output;
}
$string = 'Progress in Veterinary Science';
echo createAcronym($string, false);
Run Code Online (Sandbox Code Playgroud)
这是一个更强大和有用的函数,它支持UTF8字符和仅使用大写单词的选项:
function createAcronym($string, $onlyCapitals = false) {
$output = null;
$token = strtok($string, ' ');
while ($token !== false) {
$character = mb_substr($token, 0, 1);
if ($onlyCapitals and mb_strtoupper($character) !== $character) {
$token = strtok(' ');
continue;
}
$output .= $character;
$token = strtok(' ');
}
return $output;
}
$string = 'Leiðari í Kliniskum Útbúgvingum';
echo createAcronym($string);
Run Code Online (Sandbox Code Playgroud)
Michael Berkowski(和其他人)回答,简化为一行,并在多字节字符上正确工作(即用非拉丁字符串制作缩写/首字母):
foreach(explode(' ', $words) as $word) $acronym .= mb_substr($word, 0, 1, 'utf-8');
Run Code Online (Sandbox Code Playgroud)
如果您正在使用非拉丁语,多字节字符串和字符,即使用UTF-8编码字符串时,请使用mb_substr($word, 0, 1, 'utf-8'),而不是$word[0]似乎必须使用.
正如其他人所解释的,经典方法包括迭代初始字符串的每个单词,将单词减少到它的第一个字母,然后将这些第一个字母组合在一起。
这是结合不同步骤的辅助方法。
/**
* @return string
*/
function getInitials($string = null) {
return array_reduce(
explode(' ', $string),
function ($initials, $word) {
return sprintf('%s%s', $initials, substr($word, 0, 1));
},
''
);
}
Run Code Online (Sandbox Code Playgroud)
注意:如果给定的字符串为空,这将返回一个空字符串。
getInitials('Community College District')
字符串“CCD”(长度=3)
getInitials()
字符串''(长度=0)
getInitials('Lorem ipsum dolor sic amet')
字符串 'Lidsa'(长度 = 5)
当然,您可以向 的回调函数添加过滤器array_reduce(),例如,strtoupper()如果您只喜欢大写首字母。
$temp = explode(' ', $string);
$result = '';
foreach($temp as $t)
$result .= $t[0];
Run Code Online (Sandbox Code Playgroud)
像这样
preg_match_all('#(?<=\s|\b)\pL#u', $String, $Result);
echo '<pre>' . print_r($Result, 1) . '</pre>';
Run Code Online (Sandbox Code Playgroud)