更新时间:2022-11-16 15:01:21
explode()
在空格上,然后使用 []
符号来访问作为数组的结果字符串:
explode()
on the spaces, then you use the []
notation to access the resultant strings as arrays:
$words = explode(" ", "Community College District");
$acronym = "";
foreach ($words as $w) {
$acronym .= $w[0];
}
如果您期望多个空格可以分隔单词,请改用 preg_split()
If you have an expectation that multiple spaces may separate words, switch instead to preg_split()
$words = preg_split("/\s+/", "Community College District");
或者,如果不是空格的字符分隔单词 (-,_
),例如,也使用 preg_split()
:
Or if characters other than whitespace delimit words (-,_
) for example, use preg_split()
as well:
// Delimit by multiple spaces, hyphen, underscore, comma
$words = preg_split("/[\s,_-]+/", "Community College District");