[英]Insert space between two captalized words?
我将两个单词组合在一起,例如KitnerCoster
并且我想在中间添加一个空格,如果将两个都大写的单词分开,那是唯一的区别KitnerCoster
?
Using regular expressions you could do something like 使用正则表达式,您可以执行类似的操作
s/([a-z])([A-Z])/'$1 $2'/g
However my first trial to write a regular expression usually fails so you might have to correct one or the other part. 但是,我编写正则表达式的第一次尝试通常会失败,因此您可能必须更正其中一部分。
How do you want to handle a 1-character long word? 您要如何处理1个字符的长字? Like 'X' in FooXBar?
像FooXBar中的“ X”一样? The X will not be recognized as a separate word by the above regular expression.
上面的正则表达式不会将X识别为单独的单词。
Do you care if the first word is capitalized? 您是否关心第一个单词是否大写? If not, do
如果没有,那就
// ASCII
preg_replace('/([a-z])([A-Z])/', '$1 $2', $string)
preg_replace('/([[:lower:]])([[:upper:]])/', '$1 $2', $string)
// Unicode (UTF-8)
preg_replace('/(\p{Ll})(\p{Lu})/u', '$1 $2', $string)
If you do care, and want to fix KitnerCostner
but leave kitnerCostner
alone, then do 如果您确实需要维护,并且想修复
KitnerCostner
但kitnerCostner
,则可以
// ASCII
preg_replace('/\b([A-Z]\S*[a-z])([A-Z])/', '$1 $2', $string)
preg_replace('/\b([[:upper:]]\S*[[:lower:]])([[:upper:]])/', '$1 $2', $string)
// Unicode (UTF-8)
preg_replace('/\b(\p{Lu}\S*\p{Ll})(\p{Lu})/u', '$1 $2', $string)
I've given versions that only match ASCII letters and ones that match all Unicode characters . 我给出的版本只能匹配ASCII字母,也可以匹配所有Unicode字符 。 The Unicode ones are available in PHP 5.1.0.
Unicode编码在PHP 5.1.0中可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.