在大写字母上爆炸字符串? [英] Explode a string on upper case characters?
本文介绍了在大写字母上爆炸字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何根据大写字母将$param
字符串分解为$chunks
个片段?
Ho can i explode $param
string into $chunks
pieces based on upper-case characters?
$string = 'setIfUnmodifiedSince';
$method = substr($string, 0, 3);
$param = substr($string, 3);
// Split $param and implode with '-' separator
$chunks = splitAtUpperCase($param); // Chunks are: 'If', 'Unmodified' and 'Since'
$field = implode('-', $chunks); // Get If-Unmodified-Since HTTP field name
推荐答案
function splitAtUpperCase($s) {
return preg_split('/(?=[A-Z])/', $s, -1, PREG_SPLIT_NO_EMPTY);
}
编辑
如果您不需要数组本身,则可以使用连字符(-
)将大写字符(除了第一个字符)放在前面:
EDIT
If you don't need the array itself, you can just preprend uppercase characters (except the first) with a hyphen (-
):
preg_replace('/(?<!^)([A-Z])/', '-\\1', $param);
(演示)
这篇关于在大写字母上爆炸字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文